Quick revision
The short answer
ArrayList keeps its elements in an ordinary Object[] array plus a size field that counts how many slots are used. When the array is full, it allocates a new one about 1.5 times larger and copies the elements across. So get(i) and add at the end are O(1) (the end only amortised), while add at index 0 and remove from the middle are O(n), because the elements after that position must shift.
What to remember
In this answer, we saw that ArrayList is an Object[] array plus a size counter. A new list shares an empty array until the first add allocates 10 slots, and a full array grows by about 1.5 times with one copy. From that layout we derived the costs: get(i) is O(1), add at the end is amortised O(1), and add at index 0 or remove from the middle is O(n) because elements shift with System.arraycopy.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
ArrayList keeps its elements in an ordinary Object[] array plus a size field that counts how many slots are used. When the array is full, it allocates a new one about 1.5 times larger and copies the elements across. So get(i) and add at the end are O(1) (the end only amortised), while add at index 0 and remove from the middle are O(n), because the elements after that position must shift.
Why this question is asked
ArrayList is the default list in almost every Java codebase, so its costs are the costs of most of our code. Interviewers ask about its internals because the answer explains everyday performance decisions: why inserting at the front is slow, why we pre-size lists, and why ArrayList usually beats LinkedList. In this answer, we will open up ArrayList, follow what happens on the first add, watch the array grow, and then work out the cost of each operation from what the code has to do.
1What Is Inside an ArrayList
An ArrayList has two important fields. elementData is an Object[] that holds references to the elements, and size counts how many of its slots are in use. The length of the array is called the capacity, and it is usually larger than the size, so there is room to add more elements without allocating.
Because the elements sit in one contiguous array, the list can reach any position directly: element i is simply elementData[i]. That single fact explains most of ArrayList's behaviour.
| Field | Value |
|---|---|
| size | 3 |
elementData.length (capacity) | 10 |
elementData[0..2] | "a", "b", "c" |
elementData[3..9] | null, free room for 7 more adds |
2The First add() and the Shared Empty Array
A new ArrayList<>() does not allocate ten slots straight away. It points elementData at one shared, empty array constant. Many lists are created and never filled, and this saves memory for all of them. Only on the first add() does ArrayList notice that it still has the shared empty array and allocate a real array of the default capacity, 10.
Notice that new ArrayList<>(0) is treated differently: it uses a different empty constant, and grows from there by small steps instead of jumping to 10. That detail rarely matters, but it shows that the empty constructor is tuned for the common case.
3How the Array Grows
When add() finds the array full, it calls grow(). The new capacity is the old capacity plus half of it, computed as old + (old >> 1), so about 1.5 times. Then Arrays.copyOf allocates the bigger array and copies all the references across, and the old array becomes garbage.
- Is the array full?size ==
elementData.lengthYesGrow the array firstNoStore the element - Allocate a new arrayabout 1.5 times the old capacity
- Copy all references
Arrays.copyOf, O(n) but rare - Store at elementData[size]then size++
4 steps in order. Play them, or step through them one at a time.
Why 1.5 and not 2? Growing by a factor keeps the total copying cost linear: each element is copied only a constant number of times on average, whatever the final size. A smaller factor wastes less memory in the unused tail, at the price of a few more copies. Java chose 1.5 as a balance.
4Working Out the Cost of Each Operation
Now we can derive each complexity from the code rather than memorising it. get(i) checks the index against size and returns elementData[i]: one array read, so O(1) whatever the size.
add(e) at the end usually writes one slot, O(1). Occasionally it triggers a grow that copies n elements. Because the array grows by a factor, those copies are spread over many cheap adds, so the cost per add averaged over a long run is still constant. This is what amortised O(1) means: not every add is cheap, but adding n elements costs O(n) in total.
add(0, e) must make room at the front. Every existing element shifts one place to the right, which ArrayList does with one System.arraycopy call. That is O(n). remove(i) from the middle is the mirror image: the elements after i shift one place to the left, again O(n), and the last slot is set to null so the removed object can be garbage collected. Removing the last element shifts nothing, so it is O(1).
| Operation | Cost | Why |
|---|---|---|
get(i), set(i, e) | O(1) | direct array index |
add(e) at the end | amortised O(1) | rare grow copies n elements |
add(0, e) | O(n) | every element shifts right |
remove(i) in the middle | O(n) | elements after i shift left |
| remove last element | O(1) | nothing to shift |
contains(o), indexOf(o) | O(n) | linear search with equals() |
5What This Means in Practice
The shifting cost is real but smaller than the big O suggests, because System.arraycopy moves a contiguous block of memory very efficiently. Shifting a few thousand references takes microseconds. The cost becomes serious when we do it in a loop, for example inserting at index 0 for every element of a large input, which turns the whole job into O(n squared). In that case we add at the end and reverse once, or use an ArrayDeque, which supports cheap adds at both ends.
The growth cost matters when we know the size in advance. Adding a million elements to a default list goes through about 29 growths and copies about 2.4 million references in total. Passing the expected size to the constructor avoids all of that.
6Follow-up questions
Does ArrayList ever shrink its array on its own?
No. Removing elements lowers size but keeps the capacity, so a list that once held a million elements keeps a million-slot array after clear(). We call trimToSize() to shrink it, or copy the elements into a new list.
Why is add at the end only amortised O(1) and not plain O(1)?
Because one add in many triggers a grow that copies every element, which is O(n) for that single call. Averaged over all the adds, the copies add up to a constant amount per element, so the amortised cost is O(1). For latency-sensitive code, the occasional slow add can still matter, and pre-sizing removes it.
What is the maximum size of an ArrayList?
It is limited by the maximum array length, a little under Integer.MAX_VALUE. Growth is capped near that limit, and going beyond it throws OutOfMemoryError. Long before that, the heap usually runs out.
7Conclusion
In this answer, we saw that ArrayList is an Object[] array plus a size counter. A new list shares an empty array until the first add allocates 10 slots, and a full array grows by about 1.5 times with one copy. From that layout we derived the costs: get(i) is O(1), add at the end is amortised O(1), and add at index 0 or remove from the middle is O(n) because elements shift with System.arraycopy.
Open with
An ArrayList is backed by an Object array called elementData and a size field. The array's length is the capacity, which is usually bigger than the size. A new ArrayList with the no-argument constructor starts with a shared empty array, so it allocates nothing, and the first add allocates the default capacity of ten.
Then, if they want more
When add finds the array full, it grows it to the old capacity plus half, so about one and a half times, and copies the references over with Arrays.copyOf. That gives the sequence 10, 15, 22, 33 and so on. The costs follow from that layout. get of i is a bounds check and an array read, so O(1).
Adding at the end writes one slot, and because growth is by a factor the occasional copy averages out, so it is amortised O(1). Adding at index zero has to shift every element one place right, which is O(n), done with System.arraycopy.
Removing from the middle shifts the following elements left, also O(n), and it nulls out the last slot so the object can be collected. Removing the last element is O(1). Contains and indexOf are linear searches.
In practice I pre-size the list when I know the count, and I avoid inserting at the front in a loop because that becomes quadratic.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Collections Framework, overview, java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How does ArrayList store elements and grow? What is the time complexity of add at the...": https://scrivio-interview-library.netlify.app/java/lists/ee2f5d573a/
Java fundamentals, question 8 of 16 Back to the path
More in Lists