public void add(E e) {
//add 不用关心 lastRet 是否为-1,即不在乎之前有没有 remove 或 add
checkForComodification();
try {
int i = cursor;
AbstractList.this.add(i, e);
lastRet = -1;
cursor = i + 1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
疑问就是,为什么 add 方法一定要把 lastRet 置为-1 呢,remove 方法把 lastRet 置为-1 我理解,因为 lastRet 代表的那个元素已经被删除了。但 add 方法并不会删除元素啊,lastRet 指向元素还是存在的呢。
所以我觉得源码里,这句 lastRet = -1;可以删掉啊。
原效果:
删除后效果:
