@
by73 好吧,是我没想到这一点。`previous(); add();` 这样执行以后,如果把那句删除掉了,那么 lastRet 指向的元素就不对了,所以需要这句把 lastRet = -1。
那我现在有一个大胆的想法,如果改成这样是不是就合理了:
https://paste.ubuntu.com/p/PTBDz9qDsK/```java
public void add(E e) {
//add 不用关心 lastRet,即不在乎之前有没有 remove 或 add
checkForComodification();
try {
int i = cursor;
AbstractList.this.add(i, e);
if (lastRet = -1)//刚执行了 remove,因为现在只有 remove 可以置为-1
lastRet = -1;//lastRet 不变
else if (lastRet < cursor)//刚执行了 next
lastRet = lastRet;//lastRet 不变
else if (lastRet = cursor)//刚执行了 previous
lastRet = i + 1;//让 lastRet 也跟着移动,这样 lastRet 就能指向被移动到后面一格的那个元素了
cursor = i + 1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
// 总结一下逻辑,就可以改成下面这样
public void add(E e) {
//add 不用关心 lastRet,即不在乎之前有没有 remove 或 add
checkForComodification();
try {
int i = cursor;
AbstractList.this.add(i, e);
if ( (lastRet != -1) & (lastRet = cursor) )
lastRet = i + 1;
cursor = i + 1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
```