What is an ArrayList in Java, and how does it store its elements?
answer
- Object[] array + size counter
- Resizable, index-ordered, duplicates + nulls OK
- O(1) get/set by index
- Not thread-safe
- Program to List interface
basics
~10 sAn ArrayList is a resizable list backed by an array. You can add items without picking a size up front, and you read any item by its position (index) quickly.
solid answer
~40 sArrayList is the most common List implementation in Java. Internally it holds an Object[] array plus a size counter. Unlike a plain array, it grows automatically: when the backing array is full and you add an element, it allocates a bigger array and copies the old elements over. The array keeps the element references contiguous and in index order, so plain add(x) calls read back in the order added. It allows duplicates and nulls and supports fast index access via get(i)/set(i). It is not synchronized, so it is not thread-safe by default. You typically declare it against the List interface (List<String> list = new ArrayList<>()) so callers depend on the abstraction, not the implementation, and you can swap it later.
go deeper
Knows ArrayList is a resizable list backed by an array, ordered, allows duplicates, and read by index.
Explains the Object[] + size internals, O(1) indexed access, index order, and that it is not synchronized.
Frames it against the List interface, discusses capacity vs size, null/duplicate semantics, and when to prefer it over other implementations.
Reasons about memory layout, cache behavior, and API-design implications of exposing List vs concrete type across module boundaries.
## What is a List? A **List** is an ordered collection: each element has a position called an **index**, starting at 0. Lists typically allow **duplicates** (the same value can appear more than once), and their order is the **index order** you control: plain `add(x)` calls read back in the order added, while `add(i, x)` or `sort(c)` puts elements elsewhere. ## ArrayList specifically `ArrayList` is one concrete implementation of the `List` interface. The name comes from how it works inside: it is built on top of a plain Java **array**. An array in Java is a fixed-size block of contiguous memory — once created with length N, it cannot grow. ArrayList wraps such an array (`elementData`, of type `Object[]`) and adds two things on top: 1. A `size` field tracking how many slots are actually used (the array's length, called **capacity**, is usually larger than `size`). 2. Logic to **grow** the array when it fills up (see the resizing question). So at any moment an ArrayList of 3 elements might sit in an array of length 10 — 3 used slots, 7 spare. Each slot holds a reference; the element objects themselves live elsewhere on the heap. ## Key properties - **Indexed access**: `get(i)` and `set(i, v)` jump straight to slot `i` in the array. Because array elements sit at predictable memory addresses (`base + i * elementSize`), this is **O(1)** — constant time regardless of list size. - **Index order**: `add(x)` appends, so elements read back in the order added unless you insert at an index or sort. - **Duplicates allowed**: unlike a `Set`. - **Nulls allowed**: you can store `null`. - **Not thread-safe**: concurrent modification from multiple threads without external synchronization can corrupt it or throw. Use `Collections.synchronizedList(...)` or `CopyOnWriteArrayList` for concurrency. ## How you use it ```java List<String> names = new ArrayList<>(); names.add("Ann"); // append names.add("Bob"); String first = names.get(0); // "Ann", O(1) names.set(1, "Bea"); // replace index 1 int n = names.size(); // 2 ``` Program to the `List` interface on the left-hand side; this is the **program-to-an-interface** principle so the rest of your code does not care it is an ArrayList. ## Why it matters ArrayList is the default go-to list because indexed reads are fast and memory layout is compact and cache-friendly. Its weaknesses (shifting on middle insert/remove) are covered in the related questions.
- Why declare the variable as List rather than ArrayList?Programming to the interface lets you swap the implementation (e.g. to LinkedList or an immutable list) without touching callers, and signals you only rely on List behavior.
- Does ArrayList allow null elements?Yes, ArrayList permits null values and duplicates; it does not reject or de-duplicate them.
- Saying ArrayList has a fixed size like a plain array
- Claiming it is thread-safe by default
- Confusing it with LinkedList (no backing array there)
- Thinking capacity equals size
context
- ArrayList (Java SE 27 & JDK 27)docs.oracle.com
- List (Java SE 27 & JDK 27)docs.oracle.com