Code example for ListIterator

Methods: add, next, remove

0
      staff.addLast("Tom");
       
      // | in the comments indicates the iterator position 
 
      ListIterator<String> iterator = staff.listIterator(); // |DHRT
      iterator.next(); // D|HRT
      iterator.next(); // DH|RT
      System.out.println(staff);
 
      // Add more elements after second element 
       
      iterator.add("Juliet"); // DHJ|RT
      iterator.add("Nina"); // DHJN|RT
 
      iterator.next(); // DHJNR|T
 
      // Remove last traversed element  
 
      iterator.remove(); // DHJN|T
      
      // Print all elements 
 
      System.out.println(staff);
      System.out.println("Expected: [Diana, Harry, Juliet, Nina, Tom]");