typestar

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

  1. List.of is fixed; wrapping in ArrayList makes it editable.
  2. add, add(0, ...) and remove grow and shrink in place.
  3. contains and indexOf search by value.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 4 in Collections, step 12 of 29 in Language basics.

← Previous Next →

Lists in other languages