Friday, April 24, 2009

ConcurrentModificationException

If you try to modify a list while iterating through it, then you will encounter the ConcurrentModificationException.

The foreach loop is *is* syntactic sugar for iterating. However, you need to call remove on the iterator - which foreach doesn't give you access to.


for (SgeApAdapter apAdapter : apsToUpdate) {
if (...) {
apsToUpdate.remove(apAdapter);
}
}


It throws ConcurrentModificationException. We have to use iterator explcitly to remove item in collection.

No comments: