ArrayList has a modCount field - the number of modifications to the collection.
When the iterator() method is called, a new Itr object is Itr . It has the expectedModCount field. expectedModCount initialized with the value of modCount . When you call
li.remove("str3");
modCount increasing. When you try to access li through an iterator, it checks that expectedModCount == modCount
and if these are false throws ConcurrentModificationException
Therefore, if you receive an iterator even after changing the collection, the iterator is considered invalid and you cannot use it.
gstackoverflow Mar 11 '14 at 18:39 2014-03-11 18:39
source share