skip to content

ArrayList

A growable array with O(1) indexed access, amortized appends, and O(n) inserts or removals in the middle because elements must shift. Its contiguous, cache-friendly layout is why it is the default list in practice.

part ofJavaread it →
on this pageshow

questions

6

What is an ArrayList in Java, and how does it store its elements?

level: juniorimportance: must knowfreq 85%

answer

  1. Object[] array + size counter
  2. Resizable, index-ordered, duplicates + nulls OK
  3. O(1) get/set by index
  4. Not thread-safe
  5. Program to List interface

basics

~10 s

An 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 s

ArrayList 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

for a junior

Knows ArrayList is a resizable list backed by an array, ordered, allows duplicates, and read by index.

for a middle

Explains the Object[] + size internals, O(1) indexed access, index order, and that it is not synchronized.

for a senior

Frames it against the List interface, discusses capacity vs size, null/duplicate semantics, and when to prefer it over other implementations.

for a principal

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

open as a page

What happens if you modify an ArrayList while iterating over it, and how do you handle it correctly?

level: middleimportance: must knowfreq 78%

basics

~10 s

Changing an ArrayList's size while looping over it with its iterator usually throws ConcurrentModificationException. To remove during iteration, use the iterator's own remove() method, or skip the loop and use removeIf.

open as a page

How does an ArrayList grow when it runs out of space, and what is the cost?

level: middleimportance: must knowfreq 80%

basics

~20 s

When the internal array is full and you add an item, ArrayList makes a bigger array (about 1.5x), copies everything over, then adds the new item. That copy is occasional, so adding is fast on average.

open as a page

Why are insertion and removal in the middle of an ArrayList O(n), and when does that matter?

level: middleimportance: should knowfreq 70%

basics

~20 s

To insert or remove in the middle, ArrayList has to shift every element after that spot by one position so there are no gaps. The more elements after the spot, the more shifting, so it costs O(n).

open as a page

How does Vector differ from ArrayList in Java, and why is Vector treated as a legacy class?

level: middleimportance: should knowfreq 62%

basics

~20 s

Vector is ArrayList's older, synchronized sibling: nearly every call takes its own lock, and it doubles when full or adds a fixed capacityIncrement. It predates the Collections Framework, ArrayList suits unshared lists, and per-call locks leave compound actions unsafe, hence legacy.

open as a page

When is an ArrayList the wrong choice in Java, and what should you use instead?

level: seniorimportance: should knowfreq 75%

basics

~20 s

ArrayList is wrong when the main work is at the front (use ArrayDeque), lookup by value (HashSet or HashMap), priority or sorted order (PriorityQueue, TreeMap), sharing with writer threads (concurrent collections), or large primitive data (int[]).

open as a page