CSE142 Sample ArrayList Problems handout #19 The primary methods for manipulating an ArrayList<E> are: add(E value) appends value at end of list add(int index, E value) inserts given value at given index, shifting subsequent values right clear() removes all elements of the list get(int index) returns the value at given index remove(int index) removes and returns value at given index, shifting subsequent values left set(int index, E value) replaces value at given index with given value size() returns the number of elements in list You can also use an ArrayList in a foreach loop. For example, to examine each value of an ArrayList<String> called list, you can say: for (String s : list) { // do something with s } 1. Write a static method called maxLength that takes an ArrayList of Strings as a parameter and that returns the length of the longest String. It should return 0 if passed an empty ArrayList. 2. Write a static method called 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. 3. Write a static method called removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the Strings of even length from the list. 4. Write a static method called minToFront that takes an ArrayList of integers as a parameter and that moves the minimum value in the list to the front, otherwise preserving the order of the elements. For example, if a variable called list stores the following values: [3, 8, 92, 4, 2, 17, 9] and you make the following call: minToFront(list); The value 2 is the minimum, so the list should store the following values after the call: [2, 3, 8, 92, 4, 17, 9].
Solution to Sample ArrayList Problems 1. Two possible solutions appear below. public static int maxLength(ArrayList<String> list) { int max = 0; for (int i = 0; i < list.size(); i++) { String s = list.get(i); if (s.length() > max) { max = s.length(); } } return max; } public static int maxLength(ArrayList<String> list) { int max = 0; for (String s : list) { if (s.length() > max) { max = s.length(); } } return max; } 2. Two possible solutions appear below. public static void stutter(ArrayList<String> list) { for (int i = 0; i < list.size(); i += 2) { String s = list.get(i); list.add(i, s); } } public static void stutter(ArrayList<String> list) { for (int i = list.size() - 1; i >= 0; i--) { String s = list.get(i); list.add(i, s); } } 3. One possible solution appears below. public static void removeEvenLength(ArrayList<String> list) { int i = 0; while (i < list.size()) { String s = list.get(i); if (s.length() % 2 == 0) { list.remove(i); } else { i++; } } } 4. One possible solution appears below. public static void minToFront(ArrayList<Integer> list) { int min = 0; for (int i = 1; i < list.size(); i++) { if (list.get(i) < list.get(min)) { min = i; } } int oldValue = list.remove(min); list.add(0, oldValue); }
Stuart Reges