1 /**
2 A program that demonstrates the LinkedList class
3 */
4 public class ListDemo
5 {
6 public static void main(String[] args)
7 {
8 LinkedList staff = new LinkedList();
9 staff.addFirst("Tom");
10 staff.addFirst("Romeo");
11 staff.addFirst("Harry");
12 staff.addFirst("Diana");
13
14 // | in the comments indicates the iterator position
15
16 ListIterator iterator = staff.listIterator(); // |DHRT
17 iterator.next(); // D|HRT
18 iterator.next(); // DH|RT
19
20 // Add more elements after second element
21
22 iterator.add("Juliet"); // DHJ|RT
23 iterator.add("Nina"); // DHJN|RT
24
25 iterator.next(); // DHJNR|T
26
27 // Remove last traversed element
28
29 iterator.remove(); // DHJN|T
30
31 // Print all elements
32
33 iterator = staff.listIterator();
34 while (iterator.hasNext())
35 {
36 System.out.print(iterator.next() + " ");
37 }
38 System.out.println();
39 }
40 }