← Unit 4 · all exercises
Saved in this browser only

Keep only the even numbers

Return a new ArrayList holding only the even numbers from the original, in the same order.

java.util.ArrayList<Integer> evensOnly(java.util.ArrayList<Integer> list)

Solution.java
Loading editor…
Results

Examples

  • Solution.evensOnly(new java.util.ArrayList<>(java.util.List.of(1, 2, 3, 4))).toString() → [2, 4]
  • Solution.evensOnly(new java.util.ArrayList<>(java.util.List.of(1, 3))).toString() → []

There are also 3 hidden tests you can't see. Solve the problem, not the examples.