首先先铺垫一下
在Itr初始化(用ArrayList内部方法new出来的public Iterator<E> iterator()),时int expectedModCount = modCount
(此变量定义在java.util.AbstractList类中, 为已从结构上修改此列表的次数).
modeCount初始值为0,但是ArrayList的方法:
add(两个重载方法)都会调用方法 ensureCapacityInternal(size + 1); 会增加modCount的值;
remove remove(int index) 会直接modCount++;
remove(Object o) 会调用 fastRemove(int index) 使modCount++
其他的更改集合长度的都会改变modCount值;
再看下面的代码:
我们在调用Itr(ArrayList的)的next方法时,第一行会检测checkForComodification();
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
如果我们在遍历时(或者forPlus++遍历集合)时修改了集合的长度,那么modCount的值发生改变,则
检测不会过,直接抛并发修改异常;
而ListItr为Itr的子类但每次add时虽然修改了集合长度,但及时expectedModCount = modCount;所以用迭代器的方法在遍历时(或者forPlus++遍历集合)修改集合长度不会并发修改异常
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; //初始值为0
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();
}
}
private class ListItr extends Itr implements ListIterator<E> {
ListItr(int index) {
super();
cursor = index;
}
public boolean hasPrevious() {
return cursor != 0;
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor - 1;
}
@SuppressWarnings("unchecked")
public E previous() {
checkForComodification();
int i = cursor - 1;
if (i < 0)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i;
return (E) elementData[lastRet = i];
}
public void set(E e) {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.set(lastRet, e);
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void add(E e) {
checkForComodification();
try {
int i = cursor;
ArrayList.this.add(i, e);
cursor = i + 1;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
}