Results 1 to 8 of 8
  1. #1
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default Removing from an ArrayList while looping thru

    I started working on a section of code where I will be looping through an ArrayList looking for entries matching a certain criteria. As there may be multiple occurrences I need to continue the loop after removing an entry. That got me to wondering, what happens if I remove an entry, is the loop process smart enough to pick up with where it left off, or will it skip an entry?

    Here is a sample of what the code might look like:

    Java Code:
    int idx = -1;
    
    for ( VIN VINentry : VINrecords )
    {
    	idx++;
    	if (VINentry.getType().equals("SVC"))
    		VINrecords.remove(idx);
    }
    If the data looks like this, what will happen?

    INS
    SPC
    SVC
    SVC
    TTL

    Or, is there a better way to handle this process? The intent is to delete all ArrayList entries with a type of SVC.

    Thanks,

    Mike

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Use an iterator and its remove method.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    what happens if
    Often the fastest way is to write a test program and see.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by masijade View Post
    Use an iterator and its remove method.
    Or do it the old fashioned way and loop through the ArrayList from back to front and remove the elements you want to remove. Pencil and paper show why you have to walk from back to front.

    kind regards,

    Jos

  5. #5
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Or do it the old fashioned way and loop through the ArrayList from back to front and remove the elements you want to remove. Pencil and paper show why you have to walk from back to front.

    kind regards,

    Jos
    I understand the problem, and looping backwards through the list. I wasn't sure of how ArrayList worked when you remove an entry, if it actually compressed the list, or just marked the entry as being removed. I've seen it done both ways in different languages. Doing it the latter way would preserve the index number of the entries, the former way essentially renumbers everything after the deleted entry.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Backwards reindexes the entries, too, of course, but you'll have already covered those indexes, so it doesn't matter.

  7. #7
    pahiker's Avatar
    pahiker is offline Member
    Join Date
    Jun 2010
    Posts
    63
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Use an iterator and its remove method.
    Interesting, I've not heard of iterators before, looks easier and safer, thanks.

    I may change some of the other sections that loop through the AL to use the iterator as well. Any drawbacks to it that you may have run across?

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    The "new" or "enhanced" for loop uses an Iterator as well (effectively when, maybe not, actually), but you, as the programmer, don't have access to it. Generally, I would use the enhanced for loop, unless I will need to remove something from the list, its simply less code. Then I usually use an Iterator, also, because its simply less code, although I sometimes get sentimental and loop backwards. ;-)

    Essentially when you write
    Java Code:
    for (String s : someStringList) {
    what is actually done is the same as
    Java Code:
    Iterator<String> it = someStringList.iterator();
    while (it.hasNext()) {
      String s = it.next();

Similar Threads

  1. Looping question
    By lost_soul in forum New To Java
    Replies: 1
    Last Post: 05-11-2010, 04:34 AM
  2. Help with While and For Looping
    By gmoney8316 in forum New To Java
    Replies: 2
    Last Post: 03-03-2010, 10:54 PM
  3. Looping Help Please
    By JonnySnip3r in forum New To Java
    Replies: 5
    Last Post: 01-31-2010, 05:57 AM
  4. Looping ArrayList
    By hai789 in forum Web Frameworks
    Replies: 5
    Last Post: 05-07-2008, 03:55 AM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •