2
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.

Mo Fatty
  • 317
  • 2
  • 8
  • It should be noted that you _can_ add elements using a `ListIterator`, [see here](https://stackoverflow.com/questions/7409740/add-elements-to-a-list-while-iterating-over-it-java). – Tim Biegeleisen Oct 10 '18 at 09:16

2 Answers2

2

Does the modification using LinkedList's add method change the item the itr.next is pointing at?

No.

Calling LinkedList's add doesn't change anything in the Iterator's state. Once you call, the Iterator's next() method, and before the iterator calculates the next element to return, it will check for modification and throw the ConcurrentModificationException.

Here's the relevant code, from AbstractList$Itr:

    public E next() {
        checkForComodification(); // will throw ConcurrentModificationException
                                  // before the Iterator's state is changed
        try {
            int i = cursor;
            E next = get(i);
            lastRet = i;
            cursor = i + 1;
            return next;
        } catch (IndexOutOfBoundsException e) {
            checkForComodification();
            throw new NoSuchElementException();
        }
    }
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Thanks, that answers my question. While iterating a list, if you modify the underlying list using non-iterator methods, the iterator will not update it's next/previous pointers based on those new modifications. – Mo Fatty Oct 10 '18 at 09:36
0

A key detail is that Java only has primitives and references.

When you add something to a List or any collection, it is a copy of the reference.

If you modify the object referenced, the collection is not modified though if you print the contents it might appear so.

If you call add or remove on a collection, for LinkedList and ArrayList, the iterator isn't modified but can no longer iterate (there is one exception)

If you use a CopyOnWriteArrayList you can modify the collection and keep iterating, however the iterator doesn't see the change.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thanks. I Understand the iterator can't iterate anymore with such modification. I was only interested in verifying if the itr.next is modified or not. You answer helped me understand the state of the itr instance does not change when the underlying list is modified while iteration is happening. – Mo Fatty Oct 10 '18 at 09:37