COP 3337 Spring 2017 REMOVE ALL OCCURRENCES OF AN ITEM FROM A LIST ============================================= The method deleteAll deletes all occurrences of n from list. The method main is the driver. import java.util.*; public class DeleteAll { public static void main(String[] args) { // print the header and the explanation System.out.println("Testing deleteAll"); System.out.println("=================\n\n"); System.out.println("We form a list of names and delete all occurrences "); System.out.println(" of Coca Dan."); // form the list LinkedList names = new LinkedList(); ListIterator itr = names.listIterator(); itr.add("Coca Dan"); itr.add("Coca Dan"); itr.add("Billy"); itr.add("Coca Dan"); // print the list before and after the call System.out.println("\nThe list before:"); for (String n : names) System.out.println(n); deleteAll(names, "Coca Dan"); System.out.println("\nThe list after the change:"); for (String n : names) System.out.println(n); } // remove all items in list that are equal to n // assume that n != null public static void deleteAll(LinkedList list, T n) { ListIterator itr = list.listIterator(); while (itr.hasNext()) { // check if the last visited item is n T val = itr.next(); if (val.equals(n)) itr.remove(); } } } run: Testing deleteAll ================= We form a list of names and delete all occurrences of Coca Dan. The list before: Coca Dan Coca Dan Billy Coca Dan The list after the change: Billy BUILD SUCCESSFUL (total time: 4 seconds)