CSE142 Sample ArrayList Problems handout #24 An ArrayList is a Java class that provides resizable arrays -- ones that can grow or shrink. Like arrays, ArrayLists are indexed with an int, starting with 0. However, unlike Java arrays, the things stored in an ArrayList must be objects. The primary methods for manipulating an ArrayList are: add(Object value) appends value at end of list add(int index, Object value) inserts given value at given index, shifting subsequent values right get(int index) returns the value at given index remove(int index) removes value at given index, shifting subsequent values left set(int index, Object value) replaces value at given index with given value size() returns the number of elements in list (The sample solutions in this handout are for Java 1.4.2 ... there is a significant change in the type system for collections in Java 1.5, unfortunately. The changes haven't made their way into the Java tutorial material yet -- we recommend you learn the 1.4.2 material now, and make the transition later.) 1. ArrayLists. Write a static method maxLength that takes an ArrayList of Strings as a parameter and that returns the length of the longest String in the list. If your method is passed an empty ArrayList, it should return 0. 2. ArrayLists. Write a static method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the Strings of even length from the list. 3. ArrayLists. Write a static method stutter that takes an ArrayList of Strings as a parameter and that replaces every String with two of that String. For example, if the list stores the values ("how", "are", "you?") before the method is called, it should store the values ("how", "how", "are", "are", "you?", "you?") after the method finishes executing. 4. ArrayList. Write a static method removeDuplicates that takes as a parameter a sorted ArrayList of Strings and that eliminates any duplicates from the list. For example, suppose that an ArrayList called "list" contains the following values: [be, be, is, not, or, or, or, or, question, that, the, to, to] After calling removeDuplicates(list) the list should store the following values: [be, is, not, or, question, that, the, to] Because the values will be sorted, all of the duplicates will be grouped together. You may assume that the ArrayList contains only String values, but it might be empty. Solution to Sample ArrayList Problems 1. One possible solution appears below. public static int maxLength(ArrayList list) { int max = 0; for (int i = 0; i < list.size(); i++) { String s = (String)list.get(i); if (s.length() > max) max = s.length(); } return max; } 2. One possible solution appears below. public static void removeEvenLength(ArrayList list) { int i = 0; while (i < list.size()) { String s = (String)list.get(i); if (s.length() % 2 == 0) list.remove(i); else i++; } } 3. One possible solution appears below. public static void stutter(ArrayList list) { for (int i = 0; i < list.size(); i += 2) list.add(i, list.get(i)); } 4. ArrayList. One possible solution appears below. public static void removeDuplicates(ArrayList list) { int index = 0; while (index < list.size() - 1) { String s1 = (String)list.get(index); String s2 = (String)list.get(index + 1); if (s1.equals(s2)) list.remove(index + 1); else index++; } }
Alan Borning
Last modified: Fri Feb 25 8:30 PST 2005