Data structure ArrayList Java

ArrayList arguably would be the most used collection along with the HashMap. Many of us programmers whip up code everyday which contains atleast one of these data structures to hold objects. I have already discussed how HashMap works internally in Java, in this post I'll try to explain how ArrayList internally works in Java.

As most of us would already be knowing that ArrayList is a Resizable-array implementation of the List interface i.e. ArrayList grows dynamically as the elements are added to it. So let's try to get clear idea about the following points-

  • How ArrayList is internally implemented in Java.
  • What is the backing data structure for an ArrayList.
  • How it grows dynamically and ensures that there is always room to add elements.
Because of all these side questions it is also a very important Java Collections interview question.

Note - Code of ArrayList used here for reference is from Java 10.

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;

I am sure many of you would be thinking why transient and how about serializing an ArrayList then?
ArrayList provides its own version of readObject and writeObject methods so no problem in serializing an ArrayList and that is the reason, I think, of making this Object array as transient.

What happens when ArrayList is created

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

  • public ArrayList(int initialCapacity)- When this constructor is used we can provide some initial capacity rather than depending on the default capacity as defined in the ArrayList class.
    As example-List myList = new ArrayList(7); Code in the ArrayList class is as -public ArrayList(int initialCapacity) { if (initialCapacity > 0) { this.elementData = new Object[initialCapacity]; } else if (initialCapacity == 0) { this.elementData = EMPTY_ELEMENTDATA; } else { throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); } }

    Where EMPTY_ELEMENTDATA is defined as-

    private static final Object[] EMPTY_ELEMENTDATA = {};

    It is easy to see that, if provided capacity is greater than zero then the elementData array will be created with that capacity, in case provided capacity is zero then elementData array is initialized with an empty Object array. In that case ArrayList will grow when first element is added.

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

    Code in the ArrayList class for no-arg constructor is as given below-

    public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; }

    Where DEFAULTCAPACITY_EMPTY_ELEMENTDATA is defined as

    /** * Shared empty array instance used for default sized empty instances. We * distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when * first element is added. */ private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};

    So you can see initially it will be initialized with an empty array, it will grow only when first element is added to the list.

  • public ArrayList(Collection 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.elementData = Arrays.copyOf(elementData, size, Object[].class);

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).

Code in the Java ArrayList implementation is like this-

public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; } private void ensureCapacityInternal(int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } ensureExplicitCapacity(minCapacity); }

Where DEFAULT_CAPACITY is defined as-

private static final int DEFAULT_CAPACITY = 10; private void ensureExplicitCapacity(int minCapacity) { modCount++; // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); }

You can see here it is determined if there is a need to increase the size of the array, if yes then grow method is called.

private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); }

Note that till Java 6 the new capacity calculation used to be like this-

int newCapacity = (oldCapacity * 3)/2 + 1;

Which is changed in Java 7 to use right shift operator. With right shift operator also it will grow by 50% of old capacity.
Let's see it with the help of a small program

public class Test { public static void main(String args[]) { int a = 10; System.out.println(a>>1); } }

Output

5

If the default capacity was 10 then

int newCapacity = oldCapacity + (oldCapacity >> 1);

will return 15.

What happens when an element is removed from 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.

System.arraycopy(elementData, index+1, elementData, index, numMoved);

Here index+1 is the source position and index is the destination position. Since element at the position index is removed so elements starting from index+1 are copied to destination starting from index.

Points to note

  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.

Recommendations for learning (Udemy courses)

That's all for this topic How ArrayList Works Internally in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Related Topics

You may also like-

Data structure ArrayList Java

Certain data structures in Java can be created by you (yes you). In this example, we’ll go ahead and create an ArrayList data structure that has some of the methods that the built-in ArrayList class has.

We’ll create 2 constructors:

  • The default constructor that creates an ArrayList with a default size of 10.
  • Constructor that allows an initial size to be passed to the array.

We’ll also create a number of methods:

  • void add(Object x); A method that allows you to place an Object at the end of the ArrayList.
  • void add(int index, Object x); A method that allows you to place a value at a given location.
  • Object get(int index): Allows you to retrieve a value of the arrayList array from a given location.
  • int size(); Allows you to get the number of elements currently in the Arraylist.
  • boolean isEmpty(); Tests to see if the Arraylist is empty.
  • boolean isIn(Object x); A method that sees if a particular object exist in the arrayList.
  • int find(Object x); Returns the location of first occurrence of an Object starting from location 0.
  • void remove(Object x); Removes the first occurrence of an Object starting from location 0.

I encourage you not to look at the ArrayList built-in class. See if you can figure it out on your own. The only other restriction will be to store the Objects in an array data field. Create a test class to test the ArrayList class. Name your ArrayList class ArrayList.java so that it overwrites the built in class.

Read the notes above each method. There is a precondition that states the requirements that you’ll need to abide by before the method is called. For example, if the method requires an array of integers to be passed to it, you will need to have an array of integers ready. After the method is called, there is the post-condition. This explains what the expected output of the method will be and the steps the method takes to achieve that output.

We’ll start off with the ArrayList class. The methods that were outlined above will be added to this class.

What good is the code if you’re not going to test it. We’ll create a Driver class that’s going to test the ArrayList code.

And that’s really all their is too it. With basic Java skills, you too can create your ArrayList data structure.