Java Reference
In-Depth Information
Display 15.18
A Linked List with an Iterator (part 2 of 2)
42 i.restart( );
43 while (i.hasNext( ))
44 System.out.println(i.next( ));
45 System.out.println( );
46 }
47 }
Sample Dialogue
List contains:
coat
orange juice
shoes
Will delete the node for orange juice
List now contains:
coat
shoes
Will add one node before shoes
List now contains:
coat
socks
shoes
Changing all items to credit card.
List now contains:
credit card
credit card
credit card
The basic method for cycling through the elements in the linked list using an
iterator is illustrated by the following code from the demonstration program:
System.out.println("List now contains:");
i.restart( );
while (i.hasNext( ))
System.out.println(i.next( ));
The iterator is named i in this code. The iterator i is reset to the beginning of the list
with the method invocation i.restart() , and each execution of i.next() produces
the next data item in the linked list. After all the data items in all the nodes have been
returned by i.next( ) , the Boolean i.hasNext( ) becomes false and the while
loop ends.
 
Search WWH ::




Custom Search