Here's why:
As it is says in the Javadoc:
The iterators returned by this class's iterator and listIterator
methods are fail-fast: if the list is structurally modified at any
time after the iterator is created, in any way except through the
iterator's own remove or add methods, the iterator will throw a
ConcurrentModificationException.
This check is done in the next()
method of the iterator (as you can see by the stacktrace). But we will reach the next()
method only if hasNext()
delivered true, which is what is called by the for each to check if the boundary is met. In your remove method, when hasNext()
checks if it needs to return another element, it will see that it returned two elements, and now after one element was removed the list only contains two elements. So all is peachy and we are done with iterating. The check for concurrent modifications does not occur, as this is done in the next()
method which is never called.
Next we get to the second loop. After we remove the second number the hasNext method will check again if can return more values. It has returned two values already, but the list now only contains one. But the code here is:
public boolean hasNext() {
return cursor != size();
}
1 != 2, so we continue to the next()
method, which now realizes that someone has been messing with the list and fires the exception.
Hope that clears your question up.
Summary
List.remove()
will not throw ConcurrentModificationException
when it removes the second last element from the list.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…