Results 1 to 8 of 8
- 06-21-2010, 06:05 PM #1
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); }
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
- 06-21-2010, 06:26 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
Use an iterator and its remove method.
- 06-21-2010, 06:32 PM #3what happens if
- 06-21-2010, 07:01 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 06-21-2010, 07:22 PM #5
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.
- 06-21-2010, 07:25 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
Backwards reindexes the entries, too, of course, but you'll have already covered those indexes, so it doesn't matter.
- 06-21-2010, 07:41 PM #7
- 06-22-2010, 08:38 AM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
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) {
Java Code:Iterator<String> it = someStringList.iterator(); while (it.hasNext()) { String s = it.next();
Similar Threads
-
Looping question
By lost_soul in forum New To JavaReplies: 1Last Post: 05-11-2010, 05:34 AM -
Help with While and For Looping
By gmoney8316 in forum New To JavaReplies: 2Last Post: 03-03-2010, 11:54 PM -
Looping Help Please
By JonnySnip3r in forum New To JavaReplies: 5Last Post: 01-31-2010, 06:57 AM -
Looping ArrayList
By hai789 in forum Web FrameworksReplies: 5Last Post: 05-07-2008, 04:55 AM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 12:43 PM
Bookmarks