Take this Java code:
for (ContactsReduced curContact : allcontacts) {
..........
allcontacts.remove(curContact);
}
I do based on a condition a remove, will the foreach miss the next item in order, and do we need to step back(somehow)?
Take this Java code:
for (ContactsReduced curContact : allcontacts) {
..........
allcontacts.remove(curContact);
}
I do based on a condition a remove, will the foreach miss the next item in order, and do we need to step back(somehow)?
It's definitely advised against to tamper with the collection while iterating it. I'm not sure if Java even allows it; it might throw an exception. I know C# does...
Iterator will fail with ConcurrentModificationException. That's how collection framework is designed.
That code will break with most collections - other than a few collections such as those in java.util.concurrent
, modifying a collection while iterating over it isn't supported.
A few options:
iterator()
on the collection) instead of an enhanced for loop, and call iterator.remove()
to remove an itemList<Integer> al = new ArrayList<Integer>();
for(int i=0;i<10;i++){
al.add(i);
}
for(Integer x:al){
al.remove(x);
System.out.println(al);
}
Well, the question is interesting so I tried it in my system and this is wat i got.
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at test.Main.main(Main.java:17)
Take a look at the section on iterators on the collection interface tutorial
Use Iterator instead of the for-each construct when you need to remove the current element. The for-each construct hides the iterator, so you cannot call remove. Therefore, the for-each construct is not usable for filtering.
Note that Iterator.remove is the only safe way to modify a collection during iteration