ArrayList in java

ArrayList is a class which comes with Java Collections package and it is one of the concrete implementations of List interface. This class was introduced from Java 1.2 version onwards.

ArrayList is backed by the dynamic array in terms of the array size. This dynamic array size will grow as keep on adding the Objects/elements into the ArrayList.

ArrayList mainly used to store the elements/Objects as a List and helps to perform the operations over the stored list.

Behavior of the ArrayList Class: -

  • Stores the elements/Objects.
  • Allows to store the duplicate elements/Objects.
  • ArrayList allows to access or get an element with O (1) Complexity when tries fetch the element with index.
  • Every newly added element will go into last position in the List.
  • Allows to insert element in between already added elements

ArrayList Constructors

  • ArrayList()
  • ArrayList(int initialCapacity)
  • ArrayList(Collection<? extends E> c)

For now, do not consider about the angle brackets and element E, we have covered those in Java Generics which is on a different chapter. If you would like learn generics first, please click on the specified link.

Backed by array size will grow dynamically when programmer keeps on adding the elements or Objects to the ArrayList.

Let us consider Backed array whose size is 9 will be looks like below.

java arraylist

If we check the above image, numbers started from 0 to 8 these are called as indexes. We can insert elements or objects based on the which index need to insert.

Let us suppose if we need to insert the element at index position, by following API.

import java.util.ArrayList; public class ArrayListExample { public static void main(String[] args) { ArrayList<String> list=new ArrayList(10); list.add("Element at position Zero"); list.add("Element at position One"); list.add("Element at position two"); list.add("Element at position three"); list.add("Element at position four"); System.out.println(list.toString()); list.add(4,"inserted New Element at 4th position"); System.out.println(list.toString()); } }
Output will be like below:-

[Element at position Zero, Element at position One, Element at position two, Element at position three, Element at position four]

[Element at position Zero, Element at position One, Element at position two, Element at position three, inserted New Element at 4th position, Element at position four]

If we compare the above output, the element which is present at 4th position is moved to 5th position after adding the element at index 4.

So, if we want to add an element in between index numbers 0 and n(here n will be size of the backed array) ArrayList supports that, but whatever elements present in that index will be shifted to right direction as shown in below image.

List Switching Index

The capacity of the Array will be increased in case there is no space left for adding the new elements to ArrayList.

The following are most commonly used API's of the ArrayList:

# API Use Case
1 add(E e) New element will be added to ArrayList to the end of this list.
2 add(int index, E element) New element will added at requested index of this list.
3 E remove(int index) Removes the element present at the specified index and subsequent elements will be moved towards left and returns the removed element or object.
4 boolean contains(Object o) Checks whether given Object present in the ArrayList or not, if its present then returns true otherwise false.
5 int size() Returns the count of number of elements present at any given point of time.
6 boolean isEmpty() Checks whether in the ArrayList contains any element, if it contains returns true otherwise false.