ArrayList All Methods In Java With Example

Regular Arrays are of predetermined length, that is we have to predefine the size of array in advance, but in some cases we don’t know in advance the size of array, so there was a need to have something which can be initialized first but later its size can be changed dynamically. Hence, ArrayList class came into picture of fulfilling all these requirements.

ArrayList is a subclass of AbstractList class and it implements List Interface. It has various methods that are defined and inherited from its parent class.

Important Note: For information about ArrayList read ArrayList Tutorial as here we are only discussing ArrayList methods in full details.


ArrayList methods:

Below is the complete list of methods with example:-

1. boolean add(Object o):

  • It adds an element of Specific Object type at the end of Arraylist as no index is mentioned in the method.
  • It returns True if element is successfully added, and returns false if it is not.

The Example program of this boolean add(Object o) method is as:

import java.util.ArrayList;

public class ArrayListMethods {

public static void main(String[] args) {
    
    //Integer ArrayList
ArrayList<Integer> aList = new ArrayList<Integer>();

aList.add(5);
aList.add(11);
aList.add(17);

System.out.println("Integer Number Added in ArrayList= " + aList);

//String ArrayList
ArrayList<String> sList = new ArrayList<String>();

sList.add("Learning");
sList.add("JAVA");

System.out.println("String Added in ArrayList= "+ sList);


}

}

Output:

Integer Number Added in ArrayList= [5, 11, 17]
String Added in ArrayList= [Learning, JAVA]

2. void add(int index, Object element):

  • It adds an element of Specific Object type at the specified index of the Arraylist as  given in the argument of the method.
  • It does not return anything as its return type is void.
  • If in case the index specified is out of range it throws an IndexOutOfBoundsException.

Following program helps us to understand this method void add(int index, Object element) easily:

import java.util.ArrayList;
public class ArrayListMethods {
    public static void main(String[] args) {

        //Integer ArrayList
        ArrayList<Integer> aList = new ArrayList<Integer>(7);

        aList.add(1);
        aList.add(5);
        aList.add(9);

        // adding element 25 at 3rd position which is index 2
        aList.add(2,25);

        System.out.println("Integer List After 25 added at index 2 = " + aList);

        //String ArrayList

        ArrayList<String> sList = new ArrayList<String>();

        sList.add("JAVA");
        sList.add("At");
        sList.add("AbhiAndroid");

        //Adding Learning String at index 0 i.e. 1st position
        sList.add(0, "Learning");

        System.out.println("String After Learning Added at index 0 = " + sList);

    }

}

Output:

Integer List After 25 added at index 2 = [1, 5, 25, 9]
String After Learning Added at index 0 = [Learning, JAVA, At, AbhiAndroid]

3. boolean addAll(Collection c):

  • This method adds each element of the Specific collection type at the end of the arraylist.
  • It returns True if collection is successfully added, and returns false if it is not.
  • If the collection passed in as an argument is null then it throws Null Pointer Exception. 

Below is the example program of this boolean addAll(Collection c) method is as:

 import java.util.ArrayList;

public class ArrayListMethods {

    public static void main(String args[]) {

        //First ArrayList Created
        ArrayList<Integer> aList1 = new ArrayList<Integer>(5);

        // use add() method to add elements in the list

        aList1.add(3);
        aList1.add(5);
        aList1.add(7);

        // let us print all the elements available in aList1
        System.out.println("Printing all elements of  aList1= "+ aList1);

        //Second Arraylist Created
        ArrayList<Integer> aList2 = new ArrayList<Integer>(5);

        aList2.add(9);
        aList2.add(8);
        aList2.add(11);
        aList2.add(12);

        // let us print all the elements available in aList2
        System.out.println("Printing all elements of aList2= "+ aList2);


        // adding alist2 collections to alist1 using addAll method

        aList1.addAll(aList2);
        System.out.println("Printing all the elements of aList1 after using addAll method= "+aList1);


    }
}

Output:

Printing all elements of  aList1= [3, 5, 7]
Printing all elements of aList2= [9, 8, 11, 12]
Printing all the elements of aList1 after using addAll method= [3, 5, 7, 9, 8, 11, 12]

4. boolean addAll(int index, Collection c):

  • This methods add each element of the Specific collection type at the specified index as mentioned in the argument.
  • It returns true if collection is successfully added, and returns false if it is not.
  • If the collection passed in as an argument is null then it throws Null Pointer Exception.The program of this method is:
import java.util.ArrayList;

public class ArrayListMethods {

    public static void main(String args[]) {

        //ArrayList Created
        ArrayList<Integer> aList1 = new ArrayList<Integer>(5);

        // use add() method to add elements in the list
        aList1.add(6);
        aList1.add(4);
        aList1.add(9);

        // let us print all the elements available in aList1
        System.out.println("Printing all elements of  aList1= "+ aList1);

        ArrayList<Integer> aList2 = new ArrayList<Integer>(5);

        aList2.add(11);
        aList2.add(15);
        aList2.add(16);
        aList2.add(19);

        System.out.println("Printing all elements of aList2= "+ aList2);

        // using addAll method to add collections at specific index
        aList1.addAll(2,aList2);
        System.out.println("Printing all the elements of aList1 after using addAll method= "+ aList1);
    }
}

Output:

Printing all elements of  aList1= [6, 4, 9]
Printing all elements of aList2= [11, 15, 16, 19]
Printing all the elements of aList1 after using addAll method= [6, 4, 11, 15, 16, 19, 9]

5. void clear():

  • This method remove all the elements of the arraylist.

Below the example program clear() method shows the working of this method:

import java.util.ArrayList;

public class ArrayListMethods {

    public static void main(String args[]) {

        ArrayList<Integer> aList = new ArrayList<Integer>();

        // use add() method to add elements in the list
        aList.add(1);
        aList.add(2);
        aList.add(3);

        // let us print all the elements available in aList

        System.out.println("Printing aList items before using clear method= "+aList);
        System.out.println("Printing size of aList1= " + aList.size());

        //using clear method

        aList.clear();
        System.out.println("Printing aList element after using clear method= "+aList);
        System.out.println("size of aList1 after clear() method= " + aList.size());

    }
}

Output:

Printing aList items before using clear method= [1, 2, 3]
Printing size of aList1= 3
Printing aList element after using clear method= []
size of aList1 after clear() method= 0

6. Object clone():

  • This method returns the exact same copy of the arraylist object.

Below example helps us to understand this method easily:

import java.util.ArrayList;

public class ArrayListMethods {
    public static void main(String args[]) {

        ArrayList<String> aList1 = new ArrayList<String>();

        // use add() method to add elements in the list

        aList1.add("A");
        aList1.add("B");
        aList1.add("C");
        aList1.add("D");

        //Using clone() method to copy aList1 into a new aListCopy Arraylist

        ArrayList<String> aListCopy = (ArrayList<String>) aList1.clone();

        System.out.println("aListCopy elements copied from aList1= "+aListCopy);

    }
}

Output: 

aListCopy elements copied from aList1= [A, B, C, D]

7. boolean contains(Object element):

  • This method returns true if the calling arraylist object contains the specific element as given in the argument list, otherwise it returns false.

Below is the example program of boolean contains(Object element) method is as:

import java.util.ArrayList;

public class ArrayListMethods {

    public static void main(String args[]) {

        ArrayList<Integer> aList = new ArrayList<Integer>();

        // use add() method to add elements in the list

        aList.add(7);
        aList.add(2);
        aList.add(9);

        //Checking contains method

        boolean flag1 =  aList.contains(2);
        if (flag1 == true) {
            System.out.println("aList contains element 2");
        }else{
            System.out.println("aList doesn't contains element 2");
        }

        boolean flag2 = aList.contains(5);

        if (flag2 == true) {
            System.out.println("aList contains element 5");
        } else{
            System.out.println("aList doesn't contains element 5");
        }


    }

}

Output:

aList contains element 2
aList doesn't contains element 5

8. void ensureCapacity(int minCapacity):

  • This method ensures that the size of arraylist is not less than mentioned in the argument list, if it is less then it increases its size up to as mentioned in the argument of this method.

Below program use void ensureCapacity(int minCapacity) method:

import java.util.ArrayList;

public class ArrayListMethods {

    public static void main(String args[]) {

        ArrayList<Integer> aList = new ArrayList<Integer>(3);

        // use add() method to add elements in the list

        aList.add(5);
        aList.add(2);
        aList.add(9);

        //this method will increase the capacity to 20

        aList.ensureCapacity(20);

        System.out.println("Array List Number = " + aList);

    }
}

Output:

Array List Number = [1, 2, 3]

9. Object get(int index):

  • It returns the element present in the mentioned position in the arraylist.
  • If the index mentioned in the argument is more then the size of arraylist ,then it throws Index Out of Bound Exception.

Below is the program using get(int index) method:

import java.util.ArrayList;

public class ArrayListMethods {

    public static void main(String args[]) {

        ArrayList<Integer> aList = new ArrayList<Integer>(5);

        // use add() method to add elements in the list

        aList.add(7);
        aList.add(11);
        aList.add(13);

        System.out.println("Array List Number = " + aList);



        //  element at 2nd postion
        int value = aList.get(2);
        System.out.println("Element Retrieved at index 2 i.e. 3rd position = " + value);

    }
}

Output:

Array List Number = [7, 11, 13]
Element Retrieved at index 2 i.e. 3rd position = 13

10. int indexOf(Object o):

  • This method gives the index of the element as mentioned in the arraylist starting from zero position.
  • It will return -1 , if that element is not present in the arraylist.

Below example program use indexOf(Object o) method:

import java.util.ArrayList;

public class ArrayListMethods {

    public static void main(String args[]) {

        ArrayList<String> aList = new ArrayList<String>(5);

        // use add() method to add elements in the list

        aList.add("Learning");
        aList.add("JAVA");
        aList.add("ABHIANDROID");


        System.out.println("Array List String = " + aList);


        //  using IndexOf Method
        int value1 = aList.indexOf("JAVA");
        System.out.println("Index Retrived of JAVA = " + value1);
        
        System.out.println("AT is not present so it will print index as -1");
        
        int value2 = aList.indexOf("AT");
        System.out.println("Index Retrived of AT = " + value2);
    }

}

Output:

Array List String = [Learning, JAVA, ABHIANDROID]
Index Retrived of JAVA = 1
AT is not present so it will print index as -1
Index Retrived of AT = -1

11. int lastIndexOf(Object o):

  • This method gives the index of the element as mentioned in the arraylist starting from last position.
  • It will return -1 , if that element is not present in the arraylist.

Below example program uses lastIndexOf(Object o) method:

import java.util.ArrayList;

public class ArrayListMethods {

    public static void main(String args[]) {

        ArrayList<String> aList = new ArrayList<String>(5);

        // use add() method to add elements in the list

        aList.add("A");
        aList.add("B");
        aList.add("C");
        aList.add("D");
        aList.add("E");
        aList.add("F");
        aList.add("C");
        aList.add("D");
        
        System.out.println("ArrayList: "+aList);

        //Getting index of Last C in ArrayList

        int index1 = aList.lastIndexOf("C");

        System.out.println("index of Last C in ArrayList: " + index1);
        
        int index2 = aList.lastIndexOf("D");
        System.out.println("index of Last D in ArrayList: " + index2);

    }
}

Output:

ArrayList: [A, B, C, D, E, F, C, D]
index of Last C in ArrayList: 6
index of Last D in ArrayList: 7

12. Object remove(int index):

  • It  deletes the element from the given index from the arraylist.
  • It returns an Exception IndexOutOfBoundsException, If index specified is out of range.

Below is the example program using remove(int index) method:

import java.util.ArrayList;

public class ArrayListMethods {

    public static void main(String args[]) {

        ArrayList<String> aList = new ArrayList<String>(5);

        // use add() method to add elements in the list

        aList.add("LEARNING");
        aList.add("JAVA");
        aList.add("ABHIANDROID");
        System.out.println("String ArrayList = " + aList);

        //using Remove method

        aList.remove(2);
        System.out.println("ArrayList After Removing Element at index 2= "+ aList);

    }
}

Output:

String ArrayList = [LEARNING, JAVA, ABHIANDROID]
ArrayList After Removing Element at index 2= [LEARNING, JAVA]

13. protected void removeRange(int first, int last):

  • It deletes the group of elements from the first to last as mentioned in the argument.
  • It includes the first index and excludes the last index

Below example program use removeRange(int first, int last) method:

import java.util.ArrayList;

public class ArrayListMethods extends ArrayList{

    public static void main(String[] args) {

        ArrayListMethods aList = new ArrayListMethods();

        aList.add(3);
        aList.add(5);
        aList.add(9);
        aList.add(11);
        aList.add(15);
        aList.add(16);

        System.out.println("The Arraylist:" + aList);

       // using removerange() method to remove value of index 1 to index 2

        aList.removeRange(1,3);
        
        System.out.println("Removing Element From Index 1 to Index 2 using removeRange(1,3)");
        System.out.println("The Arraylist after using removeRange:" + aList);

    }


}

Output:

The Arraylist:[3, 5, 9, 11, 15, 16]
Removing Element From Index 1 to Index 2 using removeRange(1,3)
The Arraylist after using removeRange:[3, 11, 15, 16]

14. Object set(int index, Object element):

  • This method replaces the content at index mentioned with the element given in argument list.

Below example program use set(int index, Object element) method:

import java.util.ArrayList;

public class ArrayListMethods{

    public static void main(String[] args) {

        ArrayList<Integer> aList = new ArrayList<Integer>();

        aList.add(4);
        aList.add(8);
        aList.add(12);
        aList.add(16);
        aList.add(20);
        aList.add(24);
        System.out.println("The ArrayList:" + aList);

        // Using set() Method
        aList.set(2,333);
        System.out.println("The ArrayList after setting 333 value at index 2:" + aList);

    }
}

Output:

The ArrayList:[4, 8, 12, 16, 20, 24]
The ArrayList after setting 333 value at index 2:[4, 8, 333, 16, 20, 24]

15. int size():

  • This method returns the size of the arraylist.
  • size() methods start count with 1 not 0.

Below is the example program using size() method is:

import java.util.ArrayList;

public class ArrayListMethods{

    public static void main(String[] args) {

        ArrayList<Integer> aList = new ArrayList<Integer>();

        aList.add(1);
        aList.add(2);
        aList.add(3);
        aList.add(4);
        aList.add(5);
        aList.add(6);

        // Using size Method
        System.out.println("Size of Arraylist is :" + aList.size());

    }

}

Output:

Size of Arraylist is :6

16. Object[] toArray():

  • This method converts the ArrayList to its corresponding array.
  • This method acts as Bridge between Arraylist and Array.

Below is the example program using toArray() method:

import java.util.ArrayList;

public class ArrayListMethods {

    public static void main(String args[]) {

        ArrayList<Integer> aList = new ArrayList<Integer>();

        // use add() method to add elements in the list
        aList.add(2);
        aList.add(4);
        aList.add(6);
        aList.add(8);
        
        System.out.println("ArrayList= "+aList);

        //using toArray method
        Object[] object = aList.toArray();
        
        //Using For Loop For Printing Value in Array
        
        for(int i=0;i<object.length;i++){
        System.out.println("Value at index "+i+" of Array coverted from ArrayList= "+object[i]);
        }
        

    }
}

Output:

ArrayList= [2, 4, 6, 8]
Value at index 0 of Array coverted from ArrayList= 2
Value at index 1 of Array coverted from ArrayList= 4
Value at index 2 of Array coverted from ArrayList= 6
Value at index 3 of Array coverted from ArrayList= 8

17. void trimToSize():

  • This method decreases the size of arraylist to its actual size.
  • This method helps in saving extra memory allocated at start, and ultimately  increases the performance.

Below example program use trimToSize() method:

import java.util.ArrayList;

public class ArrayListMethods {

    public static void main(String args[]) {

        ArrayList<Integer> aList = new ArrayList<Integer>(10);

        // use add() method to add elements in the list

        aList.add(1);
        aList.add(2);
        aList.add(3);
        aList.add(4);

        // Trimming the Array List

        aList.trimToSize();

        System.out.println("Printing an ArrayList after using trimToSize method "+aList);

    }
}

Output:

Printing an ArrayList after using trimToSize method [1, 2, 3, 4]

18. Boolean isEmpty()

  • As the name specifies this method checks whether an ArrayList is empty or not.
  • If it is empty this method will return true, else it will return false

Below example program use isEmpty() method:

import java.util.ArrayList;

public class ArrayListMethods {

    public static void main(String args[]) {

        ArrayList<String> aList = new ArrayList<String>(5);

        // use add() method to add elements in the list

        aList.add("A");
        aList.add("B");
        aList.add("C");

        //IsEmpty

        boolean flag1 = aList.isEmpty();

        if(flag1==true){
            System.out.println("ArrayList is Empty");
        }

        else{
            System.out.println("ArrayList is not Empty");
        }
        
        System.out.println("Using clear() method to empty ArrayList");
        aList.clear();    
        
        //Using isEmpty() method
        boolean flag2 = aList.isEmpty();
        if(flag2==true){
            System.out.println("ArrayList is Empty");
        }

        else{
            System.out.println("ArrayList is not Empty");
        }
    }

}

Output:

ArrayList is not Empty
Using clear() method to empty ArrayList
ArrayList is Empty

19. Boolean remove(Object o):

  • This method removes first Occurrence of the element specified in the argument list from the ArrayList.

Below is the example program using remove(Object o) method:

import java.util.ArrayList;

public class ArrayListMethods {

    public static void main(String args[]) {

        ArrayList<String> aList = new ArrayList<String>(5);

        // use add() method to add elements in the list

        aList.add("A");
        aList.add("B");
        aList.add("C");
        aList.add("D");
        aList.add("E");
        aList.add("B");
        aList.add("C");

        System.out.println("Elements in Arraylist= "+ aList);

        //using Remove method

        boolean flag= aList.remove("B");

        System.out.println("ArrayList After Removing  Element “B”= "+aList);

    }
}

Output:

Elements in Arraylist= [A, B, C, D, E, B, C]
ArrayList After Removing  Element “B”= [A, C, D, E, B, C]

3 thoughts on “ArrayList All Methods In Java With Example”

  1. Very good contents helped me a lot,
    Have one suggestion if we are able to edit the code and run means it will be the perfect page to learn.