CSE142 Sample ArrayList Problems handout #20
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. 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 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.
5. 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. 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 stutter(ArrayList list) {
for (int i = 0; i < list.size(); i += 2) {
String s = list.get(i);
list.add(i, s);
}
}
3. One possible solution appears below.
public static void removeEvenLength(ArrayList 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 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++;
}
}
}
5. 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;
}
}
int oldValue = list.remove(min);
list.add(0, oldValue);
// could also have been written in one line:
// list.add(0, list.remove(min));
}