Internal Working of ArrayList in Java
What is an ArrayList?
In Java, ArrayList is a resizable array implementation. ArrayList expands dynamically, ensuring that there is always room for more elements to be added. An array of the Object class serves as the ArrayList's underpinning data structure. In Java, there are three constructors for the ArrayList class. Its own readObject and writeObject methods are available. ArrayList's Object Array is transitory. RandomAccess, Cloneable, and java.io are all implemented and Serialization capable (that are Marker Interface in Java).
Where do the elements in an ArrayList get saved?
ArrayList is an array of the Object class that is defined as follows:
Many programmers must be wondering why temporary and why not serialise an ArrayList instead.
There is no difficulty serialising an ArrayList because it has its own version of readObject and writeObject procedures, that is, why this Object array is temporary.
What happens while creating an ArrayList?
To create an ArrayList in Java, there are three constructors.
1. public ArrayList(int initialCapacity): When the constructor is used we can provide some initial capacity rather than depending on the default capacity as defined in the ArrayList class.
Code in the ArrayList class is as:
Where EMPTY_ELEMENTDATA is defined as:
It's clear that if the supplied capacity is more than zero, the elementData array will be formed with that capacity, but if the provided capacity is zero, the elementData array will be constructed with an empty Object array. When the first element is inserted, the ArrayList will expand.
2. public ArrayList(): The default constructor of ArrayList class is used to create an ArrayList as following,
Code in the ArrayList class for default constructor is as given below-
Where DEFAULTCAPACITY_EMPTY_ELEMENTDATA is defined as
As mentioned above initially it will be initialized with an empty array, it will grow only when first element is added to the list.
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. In this constructor implementation checks for the length of the collection passed as parameter, if length is greater than zero then Arrays.copyOf method is used to copy the collection to the elementData array.
How does an ArrayList automatically grow?
When we add an element to an ArrayList, it first checks to see whether the array has enough capacity to contain the new element. If it doesn't, the new capacity is computed, which is 50% higher than the previous capacity, and the array is expanded by that amount (Actually uses Arrays.copyOf which returns the original array increased to the new length).
The following Java code demonstrates the Java ArrayList implementation:
Where DEFAULT_CAPACITY is defined as-
As mentioned above, it is decided whether the array needs to be expanded, and if so, the grow function is invoked.
Here, note that until Java 6, the new capacity computation looked like this:
It will also increase by 50% of its previous capacity with the addition of a right shift operator.
Let's understand through a Java program.
If the capacity was set to 10 by default,
It will return 15
What happens if an element is removed from an ArrayList?
When items are deleted from an ArrayList in Java, the gap produced by the removal of an element must be filled in the underlying array using either remove (int I (i.e. using index) or remove (Object o). Shifting any following components to the left does this (subtracts one from their indices). It is done via the System.arrayCopy method.
The source position is index+1, while the destination position is index. Because the element at position index has been deleted, items beginning at index+1 are transferred to the destination starting at index.