From ArrayList
source (JDK 1.7):
private class Itr implements Iterator<E> {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size;
}
@SuppressWarnings("unchecked")
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
Every modifying operation on an ArrayList
increments the modCount
field (the number of times the list has been modified since creation).
When an iterator is created, it stores the current value of modCount
into expectedModCount
. The logic is:
- if the list isn't modified at all during the iteration,
modCount == expectedModCount
- if the list is modified by the iterator's own
remove()
method, modCount
is incremented, but expectedModCount
gets incremented as well, thus modCount == expectedModCount
still holds
- if some other method (or even some other iterator instance) modifies the list,
modCount
gets incremented, therefore modCount != expectedModCount
, which results in ConcurrentModificationException
However, as you can see from the source, the check isn't performed in hasNext()
method, only in next()
. The hasNext()
method also only compares the current index with the list size. When you removed the second-to-last element from the list ("February"
), this resulted that the following call of hasNext()
simply returned false
and terminated the iteration before the CME could have been thrown.
However, if you removed any element other than second-to-last, the exception would have been thrown.