Recursive ArrayList Filtering
What is printed when the following program is executed?
import java.util.*;
public class Main {
public static ArrayList<Integer> filterGreaterThan(ArrayList<Integer> list, int index, int threshold) {
if(index >= list.size()) return new ArrayList<Integer>();
ArrayList<Integer> result = filterGreaterThan(list, index + 1, threshold);
if(list.get(index) > threshold) {
result.add(0, list.get(index));
}
return result;
}
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>(Arrays.asList(5, 12, 7, 20, 3));
System.out.println(filterGreaterThan(list, 0, 10));
}
}
A
[12]
B
[5, 12, 7, 20]
C
[20, 12]
D
[12, 20]
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE