All puzzles
Per page
W
ArrayList ยท Remove while traversing

Remove the short words

Remove every word in the words list that has fewer than min letters.

If words is ["a", "an", "the", "cat"], removeShort(words, 3) leaves ["the", "cat"].

8 strips: W1 to W8

for (int i = words.size() - 1; i >= 0; i--) {
W1
public static void removeShort(ArrayList<String> words, int min) {
W2
words.remove(i);
W3
}
W4
for (int i = 0; i < words.size(); i++) {
W5
}
W6
if (words.get(i).length() < min) {
W7
}
W8

Answer key: W. Remove the short words

Order: W2, W1, W7, W3, W8, W4, W6. Extra: W5

public static void removeShort(ArrayList<String> words, int min) {
	for (int i = words.size() - 1; i >= 0; i--) {
		if (words.get(i).length() < min) {
			words.remove(i);
		}
	}
}

Extra strip: for (int i = 0; i < words.size(); i++) {. Going forward, removing "a" moves "an" to index 0, and the loop goes on to index 1. "an" is never checked and stays.