COP 3337 SECTION U08 FALL 2015 LIST PROCESSING =============== This is an example of list processing with java.util.LinkedList. package listprocessing; import java.util.LinkedList; import java.util.ListIterator; public class Main { public static void main(String[] args) { // create a list of names LinkedList staff = new LinkedList(); // use an iterator ListIterator iterator = staff.listIterator(); // the list is empty, add Ali Baba, Marquito, Papa Bill, Raju, Tornado System.out.println("We add the names Ali Baba, Marquito, Papa Bill, Raju, Tornado"); iterator.add("Ali Baba"); iterator.add("Marquito"); iterator.add("Papa Bill"); iterator.add("Raju"); iterator.add("Tornado"); // print the list System.out.println("The list is "); for ( String name : staff) { System.out.println( name + " occurs at index " + staff.indexOf(name)); } // delete the second name System.out.println("\nWe delete the second name"); iterator = staff.listIterator(); iterator.next(); // we visited Ali Baba iterator.next(); // we visited Marquito; iterator.remove(); // delete Marquito // print the updated list System.out.println("The updated list is "); for ( String name : staff) { System.out.println( name + " occurs at index " + staff.indexOf(name)); } // add 2 new names after Ali Baba System.out.println("\nWe add Hex and Jill after Ali Baba"); iterator = staff.listIterator(); iterator.next(); // we visited Ali Baba iterator.add("Hex"); // we processed Ali Baba; iterator.add("Jill"); // print the updated list System.out.println("The updated list is "); for ( String name : staff) { System.out.println( name + " occurs at index " + staff.indexOf(name)); } } } The output: ----------- We add the names Ali Baba, Marquito, Papa Bill, Raju, Tornado The list is Ali Baba occurs at index 0 Marquito occurs at index 1 Papa Bill occurs at index 2 Raju occurs at index 3 Tornado occurs at index 4 We delete the second name The updated list is Ali Baba occurs at index 0 Papa Bill occurs at index 1 Raju occurs at index 2 Tornado occurs at index 3 We add Hex and Jill after Ali Baba The updated list is Ali Baba occurs at index 0 Hex occurs at index 1 Jill occurs at index 2 Papa Bill occurs at index 3 Raju occurs at index 4 Tornado occurs at index 5 BUILD SUCCESSFUL (total time: 0 seconds)