Java Reference
In-Depth Information
1 /**
2 * This is the implementation of the LinkedListIterator.
3 * It maintains a notion of a current position and of
4 * course the implicit reference to the LinkedList.
5 */
6 private class LinkedListIterator implements ListIterator<AnyType>
7 {
8 private Node<AnyType> current;
9 private Node<AnyType> lastVisited = null;
10 private boolean lastMoveWasPrev = false;
11 private int expectedModCount = modCount;
12
13 public LinkedListIterator( int idx )
14 {
15 current = getNode( idx, 0, size( ) );
16 }
17
18 public boolean hasNext( )
19 {
20 if( expectedModCount != modCount )
21 throw new ConcurrentModificationException( );
22 return current != endMarker;
23 }
24
25 public AnyType next( )
26 {
27 if( !hasNext( ) )
28 throw new NoSuchElementException( );
29
30 AnyType nextItem = current.data;
31 lastVisited = current;
32 current = current.next;
33 lastMoveWasPrev = false;
34 return nextItem;
35 }
figure 17.30a
Iterator inner class implementation for standard LinkedList class ( continues )
The iterator maintains a current position, shown at line 8. current repre-
sents the node containing the item that is to be returned by a call to next .
Observe that when current is positioned at the endmarker, a call to next is
illegal, but the call to previous should give the first item, going backwards.
As in the ArrayList , the iterator also maintains the modCount of the list it is
iterating over, initialized at the time the iterator was constructed. This vari-
able, expectedModCount , can change only if the iterator performs a remove .
lastVisited is used to represent the last node that was visited; this is used by
Search WWH ::




Custom Search