public static void println(Object o) { System.println(o); } public void printArray(List list) { println(" " + Arrays.toString(list.toArray())); }
//Constructing simple empty list ArrayList<String> names = new ArrayList<String>(); println("After Initial Construction"); println(names); //"Adding One Element at a Time" names.add("Zoo"); names.add("Yooo"); names.add("Xoooo"); names.add("Wooo"); println("After add():"); println(names); //"Adding Many" ArrayList<String> newNames = new ArrayList<String>(Arrays.asList("Voo", "Uo", "T")); names.addAll(newNames); println("After addAll():"); println(names);
//Construct and Initialize List String[] array = {"Zoo", "Yooo", "Xoooo", "Wooo"}; ArrayList<String> names = new ArrayList<String>(Arrays.asList(array)); println("Initial List"); println(names); //Remove Elem at index=2 and index=0 names.remove(2); names.remove(0); println("After removing index=2 and index=0"); println(names); //Remove All -- clear() names.clear(); println("After clear()"); println(names);