Java 25 (LTS) reference · List and ArrayList Methods

Java List add() Method

Append an element to the end of a list, or insert it at an index, shifting later elements right.

Java 25 (LTS) Since Java 1.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

list.add(element) / list.add(index, element)

Parameters

ParameterDescription
index optional
int
Where to insert.
element
E
The value to add.

Return value

true (for add(e)).

Examples

Append and insert
import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<String> cart = new ArrayList<>();
        cart.add("tea");
        cart.add("mug");
        cart.add(0, "kettle");
        System.out.println(cart);
    }
}

Output

[kettle, tea, mug]

Run this on your computer — see “Get Started” for setup.

addAll() for many
import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>(List.of(1, 2));
        nums.addAll(List.of(3, 4));
        System.out.println(nums);
    }
}

Output

[1, 2, 3, 4]

Run this on your computer — see “Get Started” for setup.

Version history

VersionChange
Java Java 1.0Added

Was this page helpful?