// CSE 143, Winter 2009, Marty Stepp // This client program uses and tests our two List classes. // Notice that we can declare our variables using the interface type (AnythingList) // and initialize them to store the actual object type such as Linked or Array // AnythingList. Also notice that we must now supply an element type in < >. public class UseList { public static void main(String[] args) { AnythingList list = new LinkedAnythingList(); list.add("Marty"); list.add("is "); list.add("great"); System.out.println(list); AnythingList list1 = new ArrayAnythingList(); doStuff(list1); AnythingList list2 = new LinkedAnythingList(); doStuff(list2); } // Adds some elements to a list and prints them. public static void doStuff(AnythingList list) { list.add(0, -4); list.add(22); list.add(17); list.add(42); list.add(8); list.add(-5); list.add(10); System.out.println("After adding elements:"); for (int i = 0; i < list.size(); i++) { System.out.println("element " + i + " = " + list.get(i)); } // test the add(index, value) method list.add(3, 10000); list.add(0, 0); list.add(6, -456); // test the remove method list.remove(3); list.remove(0); System.out.println("After removing elements:"); for (int i = 0; i < list.size(); i++) { System.out.println("element " + i + " = " + list.get(i)); } } }