Skip to content

Java ArrayList Capacity

Java ArrayList capacity example shows what is capacity of ArrayList in Java. The example also shows how to increase the ArrayList capacity and how to check ArrayList capacity.

What is ArrayList capacity in Java?

ArrayList is a dynamic array implementation of the List interface. We do not have to worry about the size of the ArrayList when we add elements to it. It grows automatically as we add the elements to it and resizes the underlying array accordingly. Size of this internal array is the capacity of the ArrayList.

When the internal array is full and we try to add an element to the ArrayList, a new array is created with more capacity and all existing array items are copied to it.

What is the initial or default capacity of ArrayList?

When we first create an ArrayList object, the size of the internal array is 10 i.e. default initial capacity of the ArrayList.

Below given code will create an ArrayList object with an initial capacity of 10.

How to specify the capacity of ArrayList?

You can use the ArrayList constructor with initial capacity as an argument.

This constructor creates an ArrayList object with the specified initial capacity. For example,

Will create an ArrayList object with an initial capacity of 20. That means the ArrayList will be able to hold 20 elements before it needs to resize the internal array.

If you know the estimated size of the ArrayList, it is always better to specify the initial capacity when creating the ArrayList. Doing so will increase the performance of your application as it does not have to de-allocate and re-allocate the internal array when ArrayList grows beyond the capacity.

If you want to increase the capacity of existing ArrayList, use ensureCapacity method.

This method increases the capacity of the ArrayList, if required so that it can hold at least the number of elements equal to the specified capacity.

How the ArrayList capacity is calculated?

When the internal array is full, ArrayList needs to allocate the new array with more capacity, copy the existing elements to the new array and de-allocate the existing array. Exact details of the new capacity calculation are not specified but usually, it is calculated as below.

That is 150% of the existing capacity plus 1. So, for example, if the ArrayList capacity is 10 and the 11th element is added to it, the new internal array will be created with a size of (10 * 3)/2 + 1 that is 16.

Wondering why + 1? Is 150% not enough? Yes, it is in most cases. But consider the scenario of ArrayList having a capacity of 1. When you add the second element to it, the new capacity calculation would be like (1 * 3)/2 which equals 1 (i.e. same as old capacity). So 1 is added to cover this edge case scenario.

What is the difference between ArrayList size and ArrayList capacity?

ArrayList capacity is the maximum number of elements it can hold without resizing the internal array. The size of ArrayList is the number of elements it currently has. See the below example for more details.

Output

Even though we created ArrayList with a capacity of 2, the size remains 0 because we have not added any elements to it.

How to check ArrayList capacity?

There is no direct way to check ArrayList capacity. The ArrayList class maintains a private Object array named elementData. Though it is never required, you may access this private array’s length to check the capacity of the ArrayList using Java reflection for experimental purposes.

Output

Note: Output could be different for you, as exact details on the internal array growth policy is not specified by the Java specifications. A different implementation may have different growth policies.

This example is a part of the Java ArrayList tutorial with examples.

Please let me know your views in the comments section below.

About the author

8 comments

    1. Hello Niraj,

      I am glad you asked the question. I do not see 15 mentioned anywhere in the example.

      I assume you are getting 15 in the output when you run this example in your computer. If this is the case, it is also a valid output. I have also mentioned this in the example “Output could be different for you, as exact details on the internal array growth policy is not specified by the Java specifications. A different implementation may have different growth policies.”. It means that the capacity calculations can be different for different versions.

      I hope it helps. Thanks.

  1. Hello,

    Can you please explain why do we need this concept of Virtual Capacity? What happens if we don’t have have virtual capacity and why only 10 by defaults not 5 or any other values? Thank you!

  2. Getting Error:
    Exception in thread “main” java.lang.reflect.InaccessibleObjectException: Unable to make field transient java.lang.Object[] java.util.ArrayList.elementData accessible: module java.base does not “opens java.util” to unnamed module @5acf9800
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
    at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178)
    at java.base/java.lang.reflect.Field.setAccessible(Field.java:172)
    at com.collectionProgram.arraylist.Demo22.getArrayListCapacity(Demo22.java:58)
    at com.collectionProgram.arraylist.Demo22.main(Demo22.java:13)

Leave a Reply

Your email address will not be published.