Java · Dynamic array · container

ArrayList<E>

Java's growable array: O(1) get/set, amortized constant-time add at the end, linear-time insertion and removal elsewhere.

import java.util.ArrayList;
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable

Java SE 25

Operations

OperationTimeWorst caseChanges the collection
add(E)O(1) ("amortized constant time" is the documented guarantee)O(n) when the array growsYes
add(int, E)O(n) at the frontO(n) at the frontYes
get / setO(1)O(1)Yes
remove(int) / remove(Object)O(n)O(n)Yes
contains / indexOfO(n)O(n)No

"Time" shows the amortized, expected or average bound when one applies; open an operation for the full profile and its assumptions.

How it works

Representation
An Object[] elementData plus a size. The documented guarantee is only "amortized constant time" for add; OpenJDK grows to about 1.5× (observed capacities 10, 15, 22, 33, 49 on JDK 25).
Ordering
Insertion order.
Duplicates
Allowed, including null.
Mutability
Mutable. Its iterators are fail-fast: a structural change outside the iterator makes the next iterator call throw ConcurrentModificationException (best effort, not a guarantee).
Memory and ownership
Stores references; primitives are boxed (ArrayList<Integer> holds Integer objects). Capacity never shrinks on its own (trimToSize).
Equality and comparison
contains, indexOf and remove(Object) use equals(). Sorting uses Comparable or a Comparator.

Pitfalls

  • list.remove(1) on a List<Integer> removes index 1, not the value 1; use remove(Integer.valueOf(1)).
  • add(0, x) and remove(0) shift every element: O(n). Use ArrayDeque for queues.
  • Removing in a for-each loop throws ConcurrentModificationException; use removeIf or an explicit Iterator.remove().
  • List.of(...) returns an immutable list; wrap it in new ArrayList<>(...) to modify.

Alternatives

  • ArrayDeque for both ends
  • LinkedList (rarely faster in practice)
  • arrays for fixed sizes and primitives
  • List.copyOf for immutable snapshots

In other languages

References