CSE142 Sample ArrayList Problems handout #27
Recall that the primary methods for manipulating an ArrayList 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
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 minToFront that takes an ArrayList of
ints 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).
3. 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.
4. 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.
5. 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, 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. Write your solution to removeDuplicates below.
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 = list.get(i);
if (s.length() > max) {
max = s.length();
}
}
return max;
}
2. One possible solution appears below.
public static void minToFront(ArrayList list) {
int min = 0;
for (int i = 1; i < list.size(); i++) {
if (list.get(i) < list.get(min)) {
min = i;
}
}
list.add(0, list.remove(min));
}
3. One possible solution appears below.
public static void removeEvenLength(ArrayList list) {
int i = 0;
while (i < list.size()) {
if (get(i).length() % 2 == 0) {
list.remove(i);
} else {
i++;
}
}
}
4. 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));
}
5. One possible solution appears below.
public static void removeDuplicates(ArrayList list) {
int index = 0;
while (index < list.size() - 1) {
if (list.get(index).equals(list.get(index + 1))) {
list.remove(index + 1);
} else {
index++;
}
}
}