Java ArrayList Length and Size

The ArrayList in Java represents a resizable array of objects that allows us to add, remove, find, sort and replace elements. Internally, the arraylist maintains a fixed-sized array to add/maintain the objects and all its methods access this backing array for their respective operations.

ArrayList does not publicly expose the length of the backing array, and only the count of the stored elements can be retrieved using the size() method.

1. Internal Implementation of ArrayList

Internally, the ArrayList class is implemented using an array (also called a backing array). The elements that are added or removed from ArrayList are actually modified in the backing array.

public class ArrayList<E> ... {

  transient Object[] elementData;   //backing array
  private int size;                 //arraylist size
 
  //...
}

When we add objects to an arraylist, and they are stored in the backing array. The count of elements stored in the array is tracked using the size attribute.

2. ArrayList Size

The size of the ArrayList refers to the number of elements (including null values) stored in the arraylist.

  • When we add an element anywhere in the arraylist, the size is incremented by 1.
  • When we remove an element, the size is decremented by 1.
  • When we replace an element, the size is not changed.
  • When we clear an arraylist, the size is reset to 0.

Let us verify the above cases with an example. We will initialize an empty arraylist, and perform a few add/remove/clear operations. After each operation, we will check the size of the arraylist.

ArrayList<Integer> arraylist = new ArrayList<>();
Assertions.assertEquals(0, arraylist.size()); //Initial size = 0

arraylist.add(1001);
Assertions.assertEquals(1, arraylist.size()); //Add increments the size by 1

arraylist.add(1002);
Assertions.assertEquals(2, arraylist.size()); //Add increments the size by 1

arraylist.set(0, 1000);
Assertions.assertEquals(2, arraylist.size()); //replace does not change the size

arraylist.remove(1002);
Assertions.assertEquals(1, arraylist.size()); //remove decrements the size by 1

arraylist.clear();
Assertions.assertEquals(0, arraylist.size()); //clear resets the size to 0

3. ArrayList Length

The length of the arraylist is the length of the backing array. But, since the ArrayList class does not expose the initial capacity there is no way to verify the length using public APIs.

Note that the default initial capacity of arraylist is 10 which creates an empty backing array of length 10. So an empty arraylist created with the default no-argument constructor always has a backing array of length 10. We can supply the custom initial capacity (or array length) as a constructor argument.

ArrayList<Integer> arraylist = new ArrayList<>();  // backing array of length 10

ArrayList<Integer> arraylist = new ArrayList<>(64);  // backing array of length 64

4. ArrayList trimToSize()

An interesting method trimToSize() makes the length of the ArrayList instance to be the list’s current size. An application can use this operation to minimize the storage of an ArrayList instance.

The trim operation creates a new backing array with the 'size' attribute and stores the elements in the array. In the new backing array, all array indices store a value. Note that a valid value can be null also.

arraylist.trimToSize();

5. Conclusion

The Java ArrayList class does not expose the length of the backing array for simplicity purposes and only exposes the count of currently stored elements in it with the size() method. Still, it is good to know the concept of length so we can use the trimToSize() method when the backing array contains enough indices pointing to no values.

Happy Learning !!

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.