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:
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