Lists in Java
The resizable workhorse collection.
import java.util.ArrayList;
import java.util.List;
// List.of is fixed; wrapping it in ArrayList makes it editable
var todo = new ArrayList<>(List.of("write", "compile", "ship"));
todo.add("celebrate");
todo.add(0, "plan");
todo.remove("compile");
System.out.println(todo.size() + " items");
System.out.println(todo.contains("ship"));
System.out.println(todo.indexOf("ship"));
System.out.println(String.join(" -> ", todo));
How it works
List.ofis fixed; wrapping inArrayListmakes it editable.add,add(0, ...)andremovegrow and shrink in place.containsandindexOfsearch by value.
Keywords and builtins used here
newvar
The run, in numbers
- Lines
- 13
- Characters to type
- 425
- Tokens
- 129
- Three-star pace
- 95 tpm
At the three-star pace of 95 tokens a minute, this run takes about 81 seconds.
Step 2 of 4 in Collections, step 12 of 29 in Language basics.