Java ArrayList

java ArrayList

class ArrayList<>

All implemented Interfaces

Serializable , Cloneable , Iterable<E> , Collection <E> , List<E> , RandonAccess

Direct Known sub-classes

AttributeList , RoleList , RoleUnsolvedList

Java ArrayList is one of the general purpose  implementing class to List interface. Due to it’s resizable nature , ArrayList is also known as ‘Resizable-array’ implementation. All the methods in Collection and List interface are applicable to ArrayList implementation and it allows duplicate elements and null elements. The default size of an arraylist is 10 and it will increase automatically if the user enter more elements that is why it is called  resizable-array implementation. Let’s  take a deeper look into ArrayList 

Java ArrayList

  • ArrayList permits duplicate and null elements , Each ArrayList has instance capacity , this capacity is the size of the array used to store the elements . As elements are added to an ArrayList the capacity increases automatically by  using a formula
    • new capacity = (current capacity * 3/2) + 1
  • This class is almost equivalent to Vector class ( implementing class to List Interface) but except unsynchronization , Vector implementation is synchronized. Synchronization is possible with the use of Collections Synchronization wrappers.
    • List list = Collections.synchronizedList(new ArrayList());
  • When compared to LinkedList(implementing class to List interface) ArrayList is fast and reliable because , the methods size , isEmpty , get , set , iterator and listIterator operations runs in constant time while applying to elements in ArrayList.
  • The add operation runs in constant time , i.e adding n elements to arraylist takes 0(n) of time all the other operations run in linear time.
    • For Example , Adding 20 elements into an ArrayList takes same time and adding 20000 elements takes same time that is called constant time.
  • The reason behind it’s fast operations ( constant time) is , ArrayList is the only implementation class that have access to RandonAccess marker interface.
  • RandomAccess is a marker interface , which provides generic algorithms to alter their behavior to provide good performance So marker interface is used by ArrayList to indicate that it is fast and generally constant time. 
  • The iterators returned by this class ‘iterator’ and ‘listIterator’ methods are fail-fast. If the list is structurally modified at any time after the iterator is created , in any way except the iterator’s own ‘remove‘ and ‘add‘ method , the iterator will throw ConcurentModificationException. Thus in the face of concurrent modification , the iterator fails quickly and cleanly rather than risking arbitrary. 
  • ArrayList is mostly used in the retrieval operations , if the requirements are mostly in retrieving operations then ArrayList is fast and reliable. But if the frequent requirements of a program are ‘add’ and ‘remove’ operations then ArrayList is not recommended to use. 

Java ArrayList Constructors

  1. public ArrayList()

Constructs an empty arraylist with initial capacity ten and has the capability of increasing it’s size when there are more than  elements to insert. 

For Example 

ArrayList al = new ArrayList();
//creates an arraylist collection with initial capacity 10.

After inserting 10 elements the completely filled array will like below

java ArrayList

When we try try to insert the 11th element then the ArrayList will automatically increases it’s capacity by the formula 

new capacity = (current-capacity * 3/2) + 1

But the ArrayList never tries to add only the 11th element , If we try to insert the out of space element , then the ArrayList will automatically creates an extra capacity arraylist by using that formula and then the ArrayList will copy the entire arraylist  into the newly created list  (i.e from elements 1 to 10 ) and then it inserts the 11th element , old arraylist will be deleted. Now in our case the new arraylist capacity is 16 (by formula).

java arraylist new capacity picture

2. public ArrayList(int initial-capacity)

Constructs an empty list with the specified initial capacity.

For Example 

ArrayList al = new ArrayList(20);

java arraylist(int initialcapacity) constructor edited

Exceptions :


  1. IllegalArgumentException : If the specified initial capacity is negative. 

3. public ArrayList(Collection c)

Constructs a list containing the elements in the specified collection ‘ c ‘ .

For Example

java ArrayList(collection c) constructor edited



  1. NullPointerException : If the specified Collection ‘c’ is null.

Java ArrayList Methods

1.public void trimToSize()

  • This method trims the capacity of the ArrayList to be the list’s current size. An application can use this operation to minimize the storage of the ArrayList instance. 
  • For example if the capacity of the  ArrayList is 16 but the elements occupied are only 10 , when this method is called , then the ArrayList capacity will be resized to 10.

2. public void ensureCapacity(int minCapacity)

Increases the capacity of the ArrayList , if necessary only. To ensure that it can hold at least the number of elements specified by the minCapacity argument.

Java ArrayList Example 

import java.util.ArayList;

public ArrayListExample

  public static void main(String[] args)
      //creates an empty list with initial capacity specified
        ArrayList<String> al = new ArrayList<String>(5);
     //ensuring the size is '5'
      System.out.println("The size of 'al' is : " +al.size());
    //adding elements into the list 
     //printing all the elements inserted til now using forEach method
       for(String element : al)
              System.out.println("Element : " +element);

       // now retrieving element at 4th index using get method

      String retrieval = al.get(3);  
        System.out.pritnln("Element at 4th position : " +retrieval);




The size of the al is 5
Element : Java
Element : is
Element : very
Element : easy
Element : learning
Element at 4th position : learning


2 thoughts on “Java ArrayList

Leave a Reply

Your email address will not be published. Required fields are marked *