Java ArrayList addLast() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 21+

Definition

In Java 21 and later, addLast(e) appends an element to the end of an ArrayList, so it becomes the last element at index size() - 1. Nothing else moves.

Its effect is the same as the classic add(e), with one difference: addLast returns void, while add(e) returns true. The name exists so that List, Deque, and LinkedHashSet share the same sequenced-collection methods.

An instance method of java.util.ArrayList, added in Java 21. It was added to List through the new SequencedCollection interface, and ArrayList overrides it.

Java 21+ only.

addLast() comes from the SequencedCollection interface added in Java 21 (JEP 431). The examples on this page need JDK 21 or newer; on Java 8, 11, or 17 the call fails to compile with "cannot find symbol". On older versions, use add(e).

Syntax

Call addLast with the element to append (Java 21+).

Java
// Java 21+
List<String> list = new ArrayList<>(Arrays.asList("a", "b"));
list.addLast("c");      // [a, b, c]
list.add("d");          // [a, b, c, d]  (works on every Java version)
CallWhat it does
list.addLast(e)Java 21+: appends e; returns nothing.
list.add(e)Appends e and returns true; any Java version.
list.add(list.size(), e)The indexed form of the same append.
list.addFirst(e)Java 21+: inserts at the other end.

Appending is constant time on average: most calls just write into a free slot of the internal array, and only occasionally does the array have to grow.

Library declarations
Java
// java.util.ArrayList<E>  (since Java 21)
public void addLast(E element)

// Declared in SequencedCollection<E> and List<E> (Java 21)

Parameters

ParameterDescription
element (E)The element to append. May be null.

Return Value

Return ValueDescription
voidNothing is returned.

Compare with Similar Methods

Appending, and what goes with it:

MethodHow it differs
add(e)Same position, returns true; works on all versions.
addFirst(e)Java 21+: inserts at index 0 instead.
getLast()Java 21+: reads the element you just appended.
addAll(c)Appends many elements at once.

Java Examples

Example 1 — Append Steps to a Recipe

Append three steps and print them with human-friendly numbers.

Java
import java.util.ArrayList;
import java.util.List;

public class ArrayListAddLastDemo {
    public static void main(String[] args) {
        List<String> steps = new ArrayList<>();
        steps.addLast("boil water");
        steps.addLast("add pasta");
        steps.addLast("drain");

        for (int i = 0; i < steps.size(); i++) {
            System.out.println((i + 1) + ". " + steps.get(i));
        }
    }
}

How It Works

  1. Natural order. Each addLast call appends, so the recipe steps stay in the order they were written.
  2. Index loop. The for loop runs i from 0 to size() - 1 and reads each step with get(i).
  3. Human numbering. Printing i + 1 turns the zero-based indexes 0, 1, 2 into 1, 2, 3.
  4. Nothing shifts. Appending never moves existing elements, which is why addLast is cheap.

Example 2 — addLast() Returns void, add() Returns true

Store the result of add, then append with addLast and read the end.

Java
import java.util.ArrayList;
import java.util.List;

public class ArrayListAddLastReturn {
    public static void main(String[] args) {
        List<String> tags = new ArrayList<>();
        boolean ok = tags.add("java");
        tags.addLast("lists");

        System.out.println("add returned " + ok);
        System.out.println(tags);
        System.out.println("last: " + tags.getLast());
    }
}

How It Works

  1. Keep the result. add("java") returns true, which is stored in ok.
  2. Nothing to store. tags.addLast("lists") is a void method, so it is written as a plain statement; assigning it to a variable would not compile.
  3. Same position. Both calls appended, so the list is [java, lists].
  4. Read the end. getLast(), also from Java 21, returns the element that was just appended.

Example 3 — Build a List from Both Ends

Each round appends a positive number and puts its negative in front.

Java
import java.util.ArrayList;
import java.util.List;

public class ArrayListAddLastBothEnds {
    public static void main(String[] args) {
        List<Integer> line = new ArrayList<>();
        for (int i = 1; i <= 3; i++) {
            line.addLast(i);
            line.addFirst(-i);
        }
        System.out.println(line);
        System.out.println("first=" + line.getFirst() + " last=" + line.getLast());
    }
}

How It Works

  1. Two inserts per round. Each pass appends i at the end and puts -i at the front.
  2. Growing outward. After round 1 the list is [-1, 1]; after round 2 it is [-2, -1, 1, 2].
  3. Final shape. Round 3 adds 3 and -3 at the edges, which happens to leave the numbers in ascending order.
  4. The ends. getFirst() and getLast() read -3 and 3 without removing them.

Frequently Asked Questions

Java 21, through the SequencedCollection interface. On older versions use add(e).
Both append to the end. add(e) returns true; addLast(e) returns void.
Yes. Appending is constant time on average, because no existing element has to move.
You can call it, but it throws UnsupportedOperationException, because lists from List.of are unmodifiable.
Use list.add(element). It puts the element in the same place.

Next: clear()

Remove all elements at once.

clear() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful