protected transient int modcount = 0;
This property is the number of times the list has been modified. Used in the following internal and non-public classes.
Private class ITR implements Iterator<e>
First look at this inner class, implement the iterator interface.
int cursor = 0;
This variable is a cursor.
int lastret =-1;
This variable represents the subscript for the element of the last iteration. If you delete an element, then this value is-1.
int expectedmodcount = Modcount;
The initial value of this variable is the modcount above, and if the value is not the same as that of the outside Modcount, it will be judged to occur as a concurrency exception.
public Boolean hasnext () {
return cursor! = size ();
}
This method is the method to determine if there is a next element, the basis of judgment is whether the cursor variable equals the list length, if equal to the traversal is complete.
Public E Next () {
Checkforcomodification ();
try {
int i = cursor;
E next = Get (i);
Lastret = i;
cursor = i + 1;
return next;
} catch (Indexoutofboundsexception e) {
Checkforcomodification ();
throw new Nosuchelementexception ();
}
}
This method is the way the iterator iterates forward. The first call to the Checkforcomodification method is to determine whether Modcount and expectedmodcount are equal, and if not, throw an exception.
Then create the variable I, the initial value is the cursor value, the cursor initial value is 0, that is, starting from 0 iterations. Then get the next element next, call the Get method of the list, then assign the value of the Lastret variable to the value of I,cursor to I+1, and then return to next. If this procedure occurs, call the Checkforcomodification method again, and then throw the exception without this element.
public void Remove () {
if (Lastret < 0)
throw new IllegalStateException ();
Checkforcomodification ();
try {
AbstractList.this.remove (Lastret);
if (Lastret < cursor)
cursor--;
Lastret =-1;
Expectedmodcount = Modcount;
} catch (Indexoutofboundsexception e) {
throw new Concurrentmodificationexception ();
}
}
This method is the method that removes the element. The first is to determine if the lastref element is less than 0, and if it is less than 0, the element is deleted and the iteration is not resumed, so an exception is thrown. Then check that two Modcount variables are equal.
Then call this list of the deletion method, judge if Lastret is smaller than the cursor, the cursor is reduced by one, Lastref assignment is-1, because the backward traversal, Lastret This value is just the element of the iteration, the cursor increased by 1, If you delete the element just now, the cursor will be reduced back.
The last two modcount are synchronized once this process has an exception and throws an exception.
final void Checkforcomodification () {
if (modcount! = expectedmodcount)
throw new Concurrentmodificationexception ();
}
This method has just been said.
Learn Java source code together--Data structure--abstractlist abstract class (iv)