CKTIS / Java
java.util.List — an ordered collection of elements
List<E> describes an ordered collection with zero-based indexes, usually allowing duplicates. Learn how to read, add, and remove elements, and distinguish a modifiable list from an unmodifiable copy and a range view.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
List<E> list = new ArrayList<>();
List<E> values = List.of(e1, e2);
List<E> snapshot = List.copyOf(list);java.util.List<E> is an ordered collection interface that usually allows duplicates; E denotes the element type. ArrayList is a modifiable implementation. List.size() reports the element count, while List.isEmpty() tests whether the list is empty. List.get(i) reads an element; valid indexes satisfy 0 <= i < size().
List.add(e) appends an element, while List.add(i, e) inserts it at position i, also allowing i == size(). List.addAll(c) appends elements in the collection's iteration order; List.addAll(i, c) inserts them. List.set(i, e) replaces an element and returns its previous value without changing the size. List.clear() removes every element.
List.contains(o) tests membership using equality, and List.indexOf(o) returns the first index or -1. List.remove(i) removes and returns the indexed element; List.remove(o) removes the first equal occurrence and reports whether the list changed. For List<Integer>, remove(1) removes by index, whereas remove(Integer.valueOf(1)) removes by value.
List.of(...) creates an unmodifiable list; List.copyOf(c) provides an unmodifiable snapshot unaffected by later changes to the source collection's membership. Both factories reject null. Neither deeply copies elements: contained objects can still change. Mutating operations are optional in the interface; lists from these factories throw UnsupportedOperationException on attempted modification.
List.subList(from, to) returns a view of [from, to), not an independent copy. Supported changes through the view affect the backing list; subList(from, to).clear() removes the range. Structurally changing the backing list outside the view makes the view's semantics undefined. In Java 21, ArrayList throws IndexOutOfBoundsException if from < 0 or to > size(). Otherwise, if from > to, it throws IllegalArgumentException.
Examples
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> items = new ArrayList<>(List.of("A", "B"));
items.add("C");
items.addAll(List.of("D", "E"));
System.out.println(items.size() + " " + items.get(0));
System.out.println(items.set(1, "X"));
System.out.println(items.contains("X") + " " + items.indexOf("X"));
System.out.println(items.remove(0));
System.out.println(items.remove("E"));
List<String> snapshot = List.copyOf(items);
items.subList(1, 3).clear();
System.out.println(items);
System.out.println(snapshot);
items.clear();
System.out.println(items.isEmpty());
}
}Result
5 A B true 1 A true [X] [X, C, D] true
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.