I'm reading openjdk AbstractList's iterator part. I couldn't figure out what the point is to check if (lastRet < cursor), which is always true to me.
Here, lastRet is the index of the value last returned by next(), and cursor is the index of the value to be returned by next().
The code is below:
public void remove() {
if (lastRet < 0) {...} // throw exception
checkForComodification();
try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor) // WHY??????????
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch (...) {
... // some exception
}
}
Using the code in the link provided by Spotted: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/AbstractList.java#AbstractList.Itr.remove%28%29
The code appears in the nested class Itr
. There's another nested class ListItr
that extends Itr
. ListItr
adds some new methods, but does not override remove()
, which means that the code for remove()
is the same in Itr
and ListItr
.
In ListItr
, the previous()
method sets lastRet
and cursor
to the same thing. Therefore, it is not always true that lastRet < cursor
when remove()
is called. It appears to always be true for a regular Itr
, but not in a ListItr
.