ArrayList

ArrayList is the default List implementation in Java. It is backed by a dynamically resizing array — you get O(1) random access, amortised O(1) appends, and O(n) insertion/removal at arbitrary positions.

Usage

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

List<String> names = new ArrayList<>();
names.add("Ada");
names.add("Grace");
names.get(0);           // "Ada"
names.size();           // 2
names.remove("Ada");
names.contains("Grace");// true

Capacity vs size

ArrayList tracks both an internal capacity (size of the backing array) and the logical size (number of elements). When capacity is exhausted, it grows (typically 1.5×) and copies. Pre-size if you know the final count:

List<Integer> big = new ArrayList<>(100_000);  // initial capacity hint

Not thread-safe

Concurrent modification from multiple threads is unsafe and may throw ConcurrentModificationException or silently corrupt state. For thread-safe alternatives: Collections.synchronizedList(list), CopyOnWriteArrayList, or lock-protected access.

ArrayList vs array

Arrays (int[]) are fixed-size and the fastest option for primitives. ArrayList holds boxed values (Integer) and resizes automatically. For primitive performance, use IntStream or a third-party library like Eclipse Collections.