intLinkList = 2, 4, 6, 8, 10
Iterator itr = intLinkList.iterator()
Say the iterator is iterating and currently pointing at Integer 6.
itr current item = 6
itr previous item = 4
itr next item = 8
When the itr is currently pointing at Integer 6, I use the Linklists' add(Object obj, int index) method to add/insert Integer 7 between Integer 6 and Integer 8.
I understand this itr instance is invalid after this modification because the underlying list has been modified, hence modCount != expectedModCount.
My question is this: Does the modification using LinkedList's add method change the item the itr.next is pointing at? I did some reading and I know this will throw a ConcurrentModificationException. But this does not answer my question if the itr.next item is modified if the underlying list get modified while iterator is iterating.
No.
Calling
LinkedList'sadddoesn't change anything in theIterator's state. Once you call, theIterator'snext()method, and before the iterator calculates the next element to return, it will check for modification and throw theConcurrentModificationException.Here's the relevant code, from
AbstractList$Itr: