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