← Unit 4 · all exercises
Saved in this browser only

The first item

Return the first item in the list, or -1 when the list is empty. An ArrayList reads with get(0), not with square brackets.

int first(java.util.ArrayList<Integer> list)

Solution.java
Loading editor…
Results

Examples

  • Solution.first(new java.util.ArrayList<>(java.util.List.of(4, 5, 6))) → 4
  • Solution.first(new java.util.ArrayList<Integer>()) → -1

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