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"))
Call
What 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
Parameter
Description
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 Value
Description
boolean (from add(E))
Always true for an ArrayList, because the list always changes.
Append twice.add("wash car") and add("buy milk") go to the end in call order, so the list prints [wash car, buy milk].
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.
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.
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());
}
}
}
Output
add returned true
duplicates allowed: [90, 75, 90]
index 5 rejected, size is 3
How It Works
Always true. The one-argument add(90) appends and reports true, because adding to an ArrayList always changes it.
Autoboxing. The int literals 90 and 75 are boxed into Integer objects, since a List<Integer> stores objects, not primitives.
Same value twice. The second 90 is stored as a separate element; an ArrayList does not reject duplicates the way a Set does.
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.
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.
add is refused. Calling add on it throws UnsupportedOperationException, reported here by class name.
Copy into a real ArrayList.new ArrayList<>(fixed) copies both colors into a growable list, where add("blue") works.
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.