List operations
What you will learn
A List is a sequence of values whose positions matter. Use one when you need to keep an ordered shopping basket, preserve repeated items, or retrieve a value by its index. ArrayList is a useful starting implementation for ordinary mutable lists.
- Insert, replace and remove elements using the correct overload.
- Distinguish growable lists, fixed-size array views and unmodifiable lists.
- Explain indexed access and traversal costs.
Use a fresh directory per example. Save as Main.java, compile with javac --release 21 Main.java, then run java Main. Only JDK standard-library dependencies are needed.
Edit a reading list
import java.util.*;public class Main {
public static void main(String[] args) { List<String> books = new ArrayList<>(); books.add("Atlas"); books.add("Map"); books.add("Atlas"); books.add(1, "Guide"); System.out.println(books.set(2, "Poems")); System.out.println(books.get(1)); System.out.println(books); }}Expected output:
MapGuide[Atlas, Guide, Poems, Atlas]The insertion moves Map to index 2; replacing it does not change size. Duplicate Atlas entries remain.
Rules and mental model
List<E> extends Collection<E> through SequencedCollection<E> in Java 21 and later. Collection describes a group of elements, and Iterable enables enhanced for traversal. A list index starts at zero. add(e) appends; add(i,e) inserts and shifts following positions; set(i,e) replaces and returns the previous element. get and set require 0 <= i < size(); insertion also permits i == size(). ArrayList uses a resizable backing array: indexed reads and replacements are constant-time, appends amortized constant-time, and insertion/removal in the middle generally linear. LinkedList is a doubly linked list and also implements Deque; indexed traversal is linear in the distance from the nearer end. Prefer traversal rather than repeated indexing for a linked list. Size counts elements, not allocated capacity. ArrayList and LinkedList permit null; List.of and List.copyOf reject null and disallow mutation. Arrays.asList over a reference array supports replacement but not changing length. A new ArrayList<>(source) makes an independent list structure, not deep copies of elements.
Choose the correct remove overload
import java.util.*;public class Main {
public static void main(String[] args) { List<Integer> scores = new ArrayList<>(List.of(8, 1, 9, 1)); System.out.println(scores.remove(1)); System.out.println(scores.remove(Integer.valueOf(1))); System.out.println(scores); }}Expected output:
1true[8, 9]The first call returns the removed element at index 1. The second returns whether a matching value was removed.
Compare a view, a snapshot and a linked list
import java.util.*;public class Main {
public static void main(String[] args) { String[] source = {"A", "B"}; List<String> view = Arrays.asList(source); List<String> snapshot = List.copyOf(view); view.set(0, "X"); List<String> linked = new LinkedList<>(view); linked.add("C"); System.out.println(source[0]); System.out.println(snapshot); System.out.println(linked); }}Expected output:
X[A, B][X, B, C]The array view shares storage. The snapshot keeps its earlier membership; the linked list can grow independently.
Common mistakes
For List<Integer>, remove(1) selects the index overload, not the value 1. Use remove(Integer.valueOf(1)) to remove the first equal value. Also do not assume every List supports add or set.
Practice
Start with a mutable list of integers [5, 2, 5]. Insert 7 at index 1, replace index 0 with 4, then remove the value 5 using the object overload. Print the replaced value and final list.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
List operations — knowledge check
Apply the chapter contracts and reason about Java 21-compatible examples.
What does add(1, "X") do to [A, B, C]?
Which removes value 2 from List<Integer>?
What does set(0, "X") return on [A, B]?
What happens when adding an item to Arrays.asList over a String array?
Which fits frequent indexed reads?