What is an ArrayList in Java, and how does it store its elements?
answer
- Object[] array + size counter
- Resizable, insertion-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. It keeps elements in contiguous memory in insertion order, 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, insertion 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 allow **duplicates** (the same value can appear more than once) and preserve **insertion order** (the order you added items is the order you see them). ## 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. ## 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. - **Insertion order preserved**: elements stay in the order added. - **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 these in an interview costs you the question
- 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