This class is the opposite of the AbstractList class in the sense that it implements the “random access” methods (get(int index), set(int index, E element), add(int index, E element) and remove(int index)) on top of the list’s list iterator, instead of the other way around. To implement a list the programmer needs only to extend this class and provide implementations for the listIterator and size methods.
For an unmodifiable list, the programmer need only implement the list iterator’s hasNext, next, hasPrevious, previous and index methods.
For a modifiable list the programmer should additionally implement the list iterator’s set method. For a variable-size list the programmer should additionally implement the list iterator’s remove and add methods.
need to implement list iterator’s hasNext/Previous() ,next(),previous() ….. set(),add(),remove() …
transientintsize=0; // Pointer to first node. // Invariant: (first == null && last == null) || // (first.prev == null && first.item != null) transient Node<E> first;
// Pointer to last node. // Invariant: (first == null && last == null) || // (last.next == null && last.item != null) transient Node<E> last;
protectedtransientintmodCount=0;//structurally modified times
///////////////////链表操作/////////////////// // Inserts element e before non-null Node succ. voidlinkBefore(E e, Node<E> succ) { // assert succ != null; final Node<E> pred = succ.prev; final Node<E> newNode = newNode<>(pred, e, succ); succ.prev = newNode; if (pred == null) first = newNode; else pred.next = newNode; size++; modCount++;//.... }
// Unlinks non-null node x. E unlink(Node<E> x) { // assert x != null; finalEelement= x.item; final Node<E> next = x.next; final Node<E> prev = x.prev;
if (prev == null) { first = next; } else { prev.next = next; x.prev = null; }
if (next == null) { last = prev; } else { next.prev = prev; x.next = null; }
public ListIterator<E> listIterator(int index) { checkPositionIndex(index); returnnewListItr(index); }
privateclassListItrimplementsListIterator<E> { private Node<E> lastReturned;//pointer ---point to current private Node<E> next;//pointer --- point to next privateint nextIndex; privateintexpectedModCount= modCount;
public E next() { checkForComodification();//here if (!hasNext()) thrownewNoSuchElementException(); //..... lastReturned = next; next = next.next; nextIndex++; return lastReturned.item; } publicvoidremove() { checkForComodification();//here if (lastReturned == null) thrownewIllegalStateException();