All About ArrayList….!!!!!!!!!

Manjunath Katagi
3 min readFeb 5, 2022

This article focuses on below topics in line:

1) What is an ArrayList ?

2) Where does ArrayList internally store elements?

3) What happens when ArrayList is created?

4) How does ArrayList grow dynamically?

5) How remove Method works Internally in ArrayList?

1) What is an ArrayList?

ArrayList is a resizable array implementation in java. ArrayList grows dynamically and ensures that there is always a space to add elements. The backing data structure of ArrayList is an array of Object class.

ArrayList class in Java has 3 constructors. It has its own version of readObject and writeObject methods. Object Array in ArrayList is transient. It implements RandomAccess, Cloneable, java.io.Serializable.

2) Where does ArrayList internally store elements?

Basic data structure used by Java ArrayList to store objects is an array of Object Class, which is defined as follows:

transient Object[] elementData;

3) What happens when ArrayList is created?

ArrayList class in Java provides 3 constructors to create an ArrayList.

1. public ArrayList(int initialCapacity), example :- List<String> myList = new ArrayList<String>(7);

2. public ArrayList()- In case default constructor is used i.e. you will create an ArrayList as myList = new ArrayList();

3. public ArrayList(Collection<? extends E> c)- If we want to construct a list containing the elements of the specified collection we can use this constructor.

4) How does ArrayList grow dynamically ?

When we add an element to an ArrayList it first verifies whether it has that much capacity in the array to store new element or not, in case there is not then the new capacity is calculated which is 50% more than the old capacity and the array is increased by that much capacity (Actually uses Arrays.copyOf which returns the original array increased to the new length).

5) How remove Method works Internally in ArrayList ?

When elements are removed from an ArrayList in Java using either remove(int i) (i.e. using index) or remove(Object o), gap created by the removal of an element has to be filled in the underlying array. That is done by Shifting any subsequent elements to the left (subtracts one from their indices). System.arrayCopy method is used for that.

Important Points:

1. ArrayList in Java is a Resizable-array implementation of the List interface.

2. Internally ArrayList class uses an array of Object class to store its elements.

3. When initializing an ArrayList you can provide initial capacity then the array would be of the size provided as initial capacity.

4. If initial capacity is not specified then default capacity is used to create an array. Default capacity is 10.

5. When an element is added to an ArrayList it first verifies whether it can accommodate the new element or it needs to grow, in case capacity has to be increased then the new capacity is calculated which is 50% more than the old capacity and the array is increased by that much capacity.

6. When elements are removed from an ArrayList space created by the removal of an element has to be filled in the underlying array. That is done by Shifting any subsequent elements to the left.

In this article I have explained about Internal Working of ArrayList in Java.

Hope you liked the article, if you like it , please share it!!!!!

Happy Coding!!!!!!

--

--

Manjunath Katagi

Open Source Enthusiast! | Blogger! | Java Expert! | Full Stack Developer!