Java ArrayList add() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayList

Definition

In Java, add() puts a new element into an ArrayList. The one-argument form add(e) appends it to the end and returns true. The two-argument form add(index, e) inserts it at a position and shifts the element already there, and every element after it, one place to the right.

Indexes start at 0, and for inserting, any index from 0 to size() is allowed: 0 puts the element first and size() puts it last. The list grows by itself, so you never set a size in advance. Duplicates and null are both allowed.

An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe.

Syntax

Two overloads: append to the end, or insert at an index.

Java
List<String> list = new ArrayList<>();
boolean added = list.add("b");   // [b], returns true
list.add("c");                   // [b, c]
list.add(0, "a");                // [a, b, c]  (insert at index 0)
list.add(list.size(), "d");      // [a, b, c, d]  (same place as add("d"))
CallWhat it does
list.add(e)Appends e to the end; returns true.
list.add(0, e)Inserts e at the front; every element shifts right.
list.add(i, e)Inserts at index i (0 to size()).
list.add(list.size(), e)Appends too, but returns nothing.

Appending is fast (constant time on average). Inserting near the front of a big list is slower, because every later element must move one slot to the right.

Library declarations
Java
// java.util.ArrayList<E>
public boolean add(E e)
public void add(int index, E element)

// Declared in Collection<E> and List<E>

Parameters

ParameterDescription
e / element (E)The element to add. May be null; duplicates are allowed.
index (int)Only in add(int, E). Position for the new element, from 0 to size() inclusive.

Return Value

Return ValueDescription
boolean (from add(E))Always true for an ArrayList, because the list always changes.
void (from add(int, E))Nothing is returned.

Compare with Similar Methods

Other ways to put elements into an ArrayList:

MethodHow it differs
addAll(c)Adds every element of another collection in one call.
set(i, e)Replaces the element at i; the size stays the same.
addFirst(e)Java 21+: same as add(0, e).
addLast(e)Java 21+: same place as add(e), but returns void.

Java Examples

Example 1 — Build a To-Do List

Append two tasks, insert one in the middle, then add at both edges.

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

public class ArrayListAddDemo {
    public static void main(String[] args) {
        List<String> todo = new ArrayList<>();
        todo.add("wash car");
        todo.add("buy milk");
        System.out.println(todo);

        todo.add(1, "pay bills");
        System.out.println(todo);

        todo.add(0, "wake up");
        todo.add(todo.size(), "sleep");
        System.out.println(todo + " size=" + todo.size());
    }
}

How It Works

  1. Append twice. add("wash car") and add("buy milk") go to the end in call order, so the list prints [wash car, buy milk].
  2. Insert in the middle. add(1, "pay bills") puts the new task at index 1 and pushes "buy milk" from index 1 to index 2.
  3. Both edges. Index 0 makes "wake up" first, and index todo.size() (4 at that moment) is the slot just after the last element, so "sleep" lands at the end.
  4. Five tasks. Each add grew the list by exactly one element, giving a size of 5.

Example 2 — The Return Value and an Index That Is Too Big

Check what add(e) returns, store a duplicate, and try to insert past the end.

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

public class ArrayListAddIndexCheck {
    public static void main(String[] args) {
        List<Integer> scores = new ArrayList<>();
        boolean changed = scores.add(90);
        System.out.println("add returned " + changed);

        scores.add(75);
        scores.add(90);
        System.out.println("duplicates allowed: " + scores);

        try {
            scores.add(5, 60);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("index 5 rejected, size is " + scores.size());
        }
    }
}

How It Works

  1. Always true. The one-argument add(90) appends and reports true, because adding to an ArrayList always changes it.
  2. Autoboxing. The int literals 90 and 75 are boxed into Integer objects, since a List<Integer> stores objects, not primitives.
  3. Same value twice. The second 90 is stored as a separate element; an ArrayList does not reject duplicates the way a Set does.
  4. No gaps allowed. With 3 elements, the valid insert positions are 0 to 3. Index 5 would leave a hole, so add(5, 60) throws IndexOutOfBoundsException and the list stays as it was.

Example 3 — Why add() Fails on Arrays.asList

Arrays.asList gives a list that cannot grow. Copying it into a real ArrayList fixes that.

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

public class ArrayListAddFixedSize {
    public static void main(String[] args) {
        List<String> fixed = Arrays.asList("red", "green");
        try {
            fixed.add("blue");
        } catch (UnsupportedOperationException e) {
            System.out.println("Arrays.asList: " + e.getClass().getSimpleName());
        }

        List<String> colors = new ArrayList<>(fixed);
        colors.add("blue");
        System.out.println("ArrayList copy: " + colors);
        System.out.println("original: " + fixed);
    }
}

How It Works

  1. A fixed-size view. Arrays.asList returns a list backed by an array. It is a List, but not a java.util.ArrayList, and its size cannot change.
  2. add is refused. Calling add on it throws UnsupportedOperationException, reported here by class name.
  3. Copy into a real ArrayList. new ArrayList<>(fixed) copies both colors into a growable list, where add("blue") works.
  4. Independent copy. The fixed list is untouched by that add and still prints [red, green].

Frequently Asked Questions

add(e) returns a boolean, which is always true for an ArrayList. add(index, e) returns nothing (void).
Call list.add(index, element). The index can be from 0 to size(); the element already at that index and all later ones shift one place to the right.
Yes. add(null) works, and the same value can be added any number of times.
The list is fixed-size or unmodifiable, for example one created by Arrays.asList or Collections.unmodifiableList. Copy it into new ArrayList<>(...) first.
Appending to the end is fast on average; occasionally the internal array is copied into a bigger one. Inserting at index 0 shifts every element, so it gets slower as the list grows.

Next: addAll()

Add every element of another collection.

addAll() 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.

7 people found this page helpful