How Arraylist works internally in java?

How Arraylist works internally in java is one of the most typical and popular Java Interview question. Also, it is curious to know about how the easiest of the collection framework API, the ArrayList, is internally working?

In this post, we will demystify How Arraylist works internally in Java.

How data in ArrayList is getting stored internally?

Arraylist class stores the data in an one-dimentional array of java.lang.Object type. Below is the piece of declaration from the ArrayList class:

private transient Object[] elementData;

All the addition, removal and traversal happens on this array.

What is the initial size of ArrayList?

Initial size of ArrayList is 0. Size of ArrayList is the number of elements it has. It is different from length of the underlying Array.

What is the initial length of underlying Object Array then?

If we are using default constructor to create and ArrayList, then size of underlying array is 0.

If we are using constructor with argument, then the size of underlying array will be the integer number we are passing as the argument to the constructor.

Can the initial length of ArrayList be increased?

YES. As stated above, ArrayList provides a constructor with argument where we can provide the initial length of the underlying array.

public ArrayList (int paramInt)

What is the initial / default capacity of ArrayList?

Well, initial or default capacity of ArrayList is 10. Again, see the code excerpt from the ArrayList class which confirms the same:

private static final int DEFAULT_CAPACITY = 10;

Initial / default capacity comes into consideration when we create the list from default constructor. In this case, size of array becomes 10 for the first insertion into the ArrayList. Well, we will discuss the complete logic when we will discuss grow(). The length of underlying array becomes 10.

How add() method of ArrayList works internally?

Here is the code excerpt of ArrayList’s add method:

public boolean add(E paramE)


ensureCapacityInternal(this.size + 1);

this.elementData[(this.size++)] = paramE

return true;


First, it ensures the capacity of underlying array, if it can accomocate the ArrayList after adding the current element. For first element, size of arraylist will become 1, so it will ensure the underlying array to be able to accomodate 1 element.

Now, let’s see how it ensures that:

private void ensureCapacityInternal(int paramInt)


if(this.elementData == EMPTY_ELEMENTDATA)

paramInt = Math.max(10,paramInt);



if condition above will work only if the ArrayList is constructed from default constructor and we are adding the first element. In other words, if ArrayList is constructed from argumented constructor OR we have already added an element, above if condition will skips.

Why? Because, at the time of default constructor, EMPTY_ELEMENTDATA is assigned to elementData array.

private static final Object[] EMPTY_ELEMENTDATA = new Object[0];
public ArrayList(){
this.elementData = EMPTY_ELEMENTDATA

Now, the call goes to ensureExplicitCapacity with argement paramInt. Suppose of paramInt as the ‘wanted size’.

First insertion with default constructor, paramInt=10.

private void ensureExplicitCapacity(int paramInt)


if(paramInt-this.elementData.length <=0)






if condition above means that if size of the underlying array is greater than ‘wanted size’, the return (do nothing).
If not, then GROW.

First insertion with default constructor if (10 -0) <=0 => false. GROW.

Here is the grow(), in case it goes for grow:

private void grow(int paramInt){

int i = this.elementData.length;

int j = i+ (i>>1);

if(j-paramInt <0)


if(j-2147483639 >0)


this.elementdata  = Arrays.copyOf(this.elementData,j);


i is size of underlying array. First insertion with default constructor i=0;

j is the target size of underlying array. j is 1.5 times i. How?

When left shift of 1 will make a number half. i>>1 will make i/2.

j= i+i/2 = 1.5i

First insertion with default constructor j = 0.
if(j-paramInt <0) says that if target is still short of wanted size then make make the target size = wanted size.

First insertion with default constructor j= 10

Then it checks for MAX Size to which an array could possibly grow.

hugeCapacity(), increases the size by 8 of MAX_SIZE.

What is the MAX _SIZE of underlying Array in ArrayList?

2147483639. hugeCapacity makes it upto 2147483647.

How does ArrayList creates new array while growing?

ArrayList uses Arrays.copyOf() to copy the current data to new Array. This function return the array with new size.

Internally, Arrays.copyOf() calls System.arraycopy() which is a native function.

While removing the element from ArrayList, does the length of underlying array decreases?

NO. Length remains same. Target data gets removed and data gets shifted. That’s all.


Let me know your feedback  or questions in the comment section.

Ankush Pahwa is an IT Majdoor (Labourer) in Indian IT industry for last 5 years. Don’t know what else to say, so stopping here.

Self-Learning -> Focused Group -> Learning Intensification -> Self-Learning

Have any Question or Comment?

One comment on “How Arraylist works internally in java?

when array list size exceeded from its capacity


Leave a Reply

Your email address will not be published. Required fields are marked *

LinkedIn Auto Publish Powered By :