Java Reference
In-Depth Information
Display 15.18
Using an Iterator
(part 1 of 2)
1
public class
IteratorDemo
2 {
3
public static void
main(String[] args)
4 {
5 LinkedList2 list =
new
LinkedList2( );
6 LinkedList2.List2Iterator i = list.iterator( );
7 list.addToStart("shoes");
8 list.addToStart("orange juice");
9 list.addToStart("coat");
10 System.out.println("List contains:");
11 i.restart( );
12
while
(i.hasNext( ))
13 System.out.println(i.next( ));
14 System.out.println( );
15 i.restart( );
16 i.next( );
17 System.out.println("Will delete the node for " + i.peek( ));
18 i.delete( );
19 System.out.println("List now contains:");
20 i.restart( );
21
while
(i.hasNext( ))
22 System.out.println(i.next( ));
23 System.out.println( );
24 i.restart( );
25 i.next( );
26 System.out.println("Will add one node before " + i.peek( ));
27 i.addHere("socks");
28 System.out.println("List now contains:");
29 i.restart( );
30
while
(i.hasNext( ))
31 System.out.println(i.next( ));
32 System.out.println( );
33 System.out.println("Changing all items to credit card.");
34 i.restart( );
35
while
(i.hasNext( ))
36 {
37 i.changeHere("credit card");
38 i.next( );
39 }
40 System.out.println( );
41 System.out.println("List now contains:");
(continued)