首先要知道的是(如
JLS所述)以下增强的for循环:
for (String s : list) {
// Do something with s
}
相当于:
for (Iterator it = list.iterator(); it.hasNext();) {
String s = it.next();
// Do something with s
}
如果你看一下AbstractList中迭代器的实现,你会看到:
> hasNext()不检查并发修改,只是使用其大小检查我们是否在列表的末尾:
public boolean hasNext() {
return cursor != size();
}
> next()完成的第一件事是调用checkForComodification()以查看在迭代时是否修改了列表:
public E next() {
checkForComodification();
try {
E next = get(cursor);
lastRet = cursor++;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
因此,当您迭代并删除列表的倒数第二个元素时,下一条指令将是对hasNext()的调用,它将返回false,因为删除一个元素导致列表的大小减少一个,并且您的迭代将停止而不调用next()并抛出异常.
顺便说一句,所有这些只是一个实现细节,你不应该依赖它,因为它可以改变,并在你迭代时使用it.remove()从列表中删除一个元素.