What is ArrayList in Java?

Wondering how ArrayList java could be useful , see the below conversation -

How to use Java Arraylist

See the following picture of a man stretching an elastic rubber band.

The actual length of the rubber band is much smaller but when stretched it can extend a lot more than its actual length and can be used to hold/bind much larger objects with it.

Now, consider the next picture, that of a simple rope, it cannot stretch and will have a fixed length. How to use Java Arraylist


It can grow as and when required to accommodate the elements it needs to store and when elements are removed, it can shrink back to a smaller size.

So as our friend has an issue like the array he is using cannot be expanded or made to shrink, we will definitely be using ArrayList.

Arrays are like the rope shown in the above picture; they will have a fixed length, cannot be expanded nor reduced from the original length.

So our stretchable rubber-band is much like the Array List whereas the rope can be considered as the array.

Technically speaking, java Array List is like a dynamic array or a variable-length array.

Let us see and understand the following code snippet that will help you working around with Array List.

ArrayList<Object> a = new ArrayList<Object>();

Arraylist Methods

  • Arraylist add: This is used to add elements to the Array List. If an ArrayList already contains elements, the new element gets added after the last element unless the index is specified.
  • Arraylist remove: The specified element is removed from the list and the size is reduced accordingly. Alternately, you can also specify the index of the element to be removed.
  • Java array size:  This will give you the number of elements in the Array List. Just like arrays, here too the first element starts with index 0.
  • Arraylistcontains:  This method will return true if the list contains the specified element.

Java Arraylist Example

import java.util.ArrayList;
class Test_ArrayList {
    public static void main(String[] args) {
        //Creating a generic ArrayList
        ArrayList arlTest = new ArrayList();
        //Size of arrayList
        System.out.println("Size of ArrayList at creation: " +  arlTest.size());
        //Lets add some elements to it
        //Recheck the size after adding elements
        System.out.println("Size of ArrayList after adding elements: " + arlTest.size());
        //Display all contents of ArrayList
        System.out.println("List of all elements: " + arlTest);
        //Remove some elements from the list
        System.out.println("See contents after removing one element: " + arlTest);
        //Remove element by index
        System.out.println("See contents after removing element by index: " + arlTest);
        //Check size after removing elements
        System.out.println("Size of arrayList after removing elements: " + arlTest.size());
        System.out.println("List of all elements after removing elements: " + arlTest);
        //Check if the list contains "K"

Note: For simplicity, the elements shown in above code are single character elements. We can add Strings, integers etc too.

How to use Java Arraylist