A Complete Java ArrayList Tutorial

The tutorial explains everything about ArrayList in Java. Learn how to use Java ArrayList methods and properties.

Java ArrayList 

 
The array is a collection of elements of the same data type, stored in a contiguous memory location. A list is a non-linear collection of data where the data stored can be of different types, and also, the data is stored in non-contiguous memory locations.
 
An array has a hardcoded limit, i.e., the size of an array is always predefined which can't be increased dynamically. A list, on the other hand, has a variable limit, i.e., the size of a list can be increased dynamically.
 
ArrayList in Java is the list of array. An ArrayList combines the properties of List and of an Array, i.e., in ArrayList, the size is varied dynamically at runtime which also indicates that instead of a stack, a heap is used.
 
Java is completely an object-oriented language, hence everything in Java is a class. And each class is stored in a given package. The ArrayList class is stored in the java.util package. 
 
The method to use and implement ArrayList Class has been demonstrated below.     
  1. public class Csharpcorner {  
  2.  public static void main(String[] args) {  
  3.   ArrayList < String > cars = new ArrayList < String > ();  
  4.  }  
  5. }   
In the above code, we are creating an object of ArrayList class. The above array list will store all the elements as String.
 

ArrayList Constructors 

  1. Arraylist.ArrayList()
      
    Used to build an empty ArrayList.
    1. public class Csharpcorner {  
    2.  public static void main(String[] args) {  
    3.   ArrayList cars = new ArrayList();  
    4.  }  
    5. }    
  2. Arraylist.ArrayList<Collection c>()
     
    This constructor is used to build an ArrayList initialized with the elements from the collection c.
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   ArrayList < String > cars = new ArrayList < String > ();  
    5.  }  
    6. }  
  3. ArrayList(int capacity)
     
    This constructor is used to build an ArrayList with initial capacity being specified.
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   int capacity = 6;  
    5.   ArrayList < String > cars = new ArrayList < String > (capacity);  
    6.  }  
    7. }  

 

Features of ArrayList

 
  1. The ArrayList class has predefined some methods to access and modify ArrayList Elements.
  2. The size of an ArrayList can be increased or decreased whenever needed.
  3. ArrayList only supports object entries, not the primitive data types.
  4. In ArrayList, the actual objects are never stored at contiguous locations. References of the actual objects are stored at contiguous locations. 
     

ArrayList Methods

 
  1.  Arraylist.add()
      
    Syntax
     
    a. void add(int index, E element)
     
    This version of the method inserts the specified element E at the specified position in this list. It shifts the element currently at that position (if any) and any subsequent elements to the right (will add one to their indices).
     
    b. boolean add(E e)
     
    This version of the method appends the specified element to the end of this list.
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   ArrayList < String > cars = new ArrayList < String > ();  
    5.   cars.add("Ford");  
    6.  }  
    7. }  
    This code will add "Ford" as the first element of the String ArrayList, i.e. now the size of has increased to one which was initially zero. In the same way.
     
  2. Arraylist.addAll()
     
    Syntax
     
    a. boolean addAll(Collection c)
     
    This version of AddAll() is used to append all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.
     
    b. boolean addAll(int index, Collection c)
     
    This version of AddAll() is used to append all the elements in the specified collection, starting at the specified position of the list.
    1. import java.util.ArrayList; // import the ArrayList class               
    2.   
    3. public class Csharpcorner {  
    4.  public static void main(String[] args) {  
    5.   ArrayList < String > cars = new ArrayList < String > (); // Create an ArrayList object              
    6.   
    7.   cars.add("Ford");  
    8.   cars.add("Ferrari");  
    9.   cars.add("Maruti");  
    10.   cars.add("Vokesvagen");  
    11.   
    12.   ArrayList < String > new_cars = new ArrayList < String > ();  
    13.   
    14.   new_cars.add("Bukati");  
    15.   
    16.   new_cars.addAll(cars);  
    17.  }  
    18. }  
    In the above program, we are creating two ArrayLists with name cars and new_cars respectively. And then we will copy all the elements of cars into new_cars.
     
  3. Arraylist.clear()
     
    Syntax
     
    void clear()
     
    It is used to remove all of the elements from this list.
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   ArrayList < String > cars = new ArrayList < String > ();  
    5.   cars.add("Ford");  
    6.   cars.add("Ferrari");  
    7.   cars.add("Maruti");  
    8.   cars.add("Vokesvagen");  
    9.   cars.clear();  
    10.  }  
    11. }  
    In the above code, the clear Method will delete all the inserted values for the ArrayList cars
     
  4. Arraylist.ensureCapacity()
     
    Syntax
     
    void ensureCapacity(int requiredCapacity)
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   ArrayList < String > cars = new ArrayList < String > ();  
    5.   ArrayList < String > cars = new ArrayList < String > ();  
    6.   cars.add("Ford");  
    7.   cars.add("Ferrari");  
    8.   cars.add("Maruti");  
    9.   cars.add("Vokesvagen");  
    10.   cars.ensureCapacity(5000);  
    11.  }  
    12. }  
    The above code will make sure that cars can hold up to 5000 elements.
     
  5. Arraylist.Get()
     
    Syntax
     
    E get(int index)
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   ArrayList < String > cars = new ArrayList < String > ();  
    5.   cars.add("Ford");  
    6.   cars.add("Ferrari");  
    7.   cars.add("Maruti");  
    8.   cars.add("Vokesvagen");  
    9.   String c = cars.get(2);  
    10.  }  
    11. }  
    The above code will fetch the third element i.e. "Maruti" from cars
     
  6. Arraylist.isEmpty()
     
    Syntax
     
    boolean isEmpty()
     
    It returns true if the list is empty, otherwise false.
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   ArrayList < String > cars = new ArrayList < String > ();  
    5.   cars.add("Ford");  
    6.   cars.add("Ferrari");  
    7.   cars.add("Maruti");  
    8.   cars.add("Vokesvagen");  
    9.   ans = cars.isEmpty();  
    10.   if (ans == true)  
    11.    System.out.println("it is empty");  
    12.   else  
    13.    System.out.println("It is not empty");  
    14.  }  
    15. }  
    The above code will check whether the ArrayList cars is empty or not. If it is empty then it will print "It is empty".
     
  7. Arraylist.LastIndexOf()
     
    Syntax
     
    int lastIndexOf(Object o)
     
    It is used to return the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   ArrayList < String > cars = new ArrayList < String > ();  
    5.   cars.add("Ford");  
    6.   cars.add("Ferrari");  
    7.   cars.add("Maruti");  
    8.   cars.add("Vokesvagen");  
    9.   int element = arr.lastIndexOf("Maruti");  
    10.   if (element != -1)  
    11.    System.out.println("the lastIndexof of" + " Maruti is " + element);  
    12.   else  
    13.    System.out.println("Maruti is not present in" + " the list");  
    14.  }  
    15. }  
    The above code will check the latest occurrence of the element "Maruti", and returns the integer index
     
  8. Arraylist.toArray()
     
    Syntax
     
    a.    Object[] toArray()
     
    b.    <T> T[] toArray(T[] a)
     
    It is used to return an array containing all of the elements in this list in the correct order.
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   ArrayList < String > cars = new ArrayList < String > ();  
    5.   cars.add("Ford");  
    6.   cars.add("Ferrari");  
    7.   cars.add("Maruti");  
    8.   cars.add("Vokesvagen");  
    9.   Object[] objects = cars.toArray();  
    10.  }  
    11. }     
    The above code will convert the ArrayList object into an array of type Object
     
  9. Arraylist.clone()
     
    Syntax
     
    Object clone()
     
    It is used to return a shallow copy of an ArrayList.
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   ArrayList < String > cars = new ArrayList < String > ();  
    5.   cars.add("Ford");  
    6.   cars.add("Ferrari");  
    7.   cars.add("Maruti");  
    8.   cars.add("Vokesvagen");  
    9.   ArrayList < String > new_cars = new ArrayList < String();  
    10.   new_cars = cars.clone();  
    11.  }  
    12. }  
    A shallow copy means that when we are creating a copy we are not assigning memory to the new copy, instead of that we are using the new copy as a pointer to which we pass the address of the original ArrayList.
     
    So in the above code, we are passing the address of cars to new_cars
     
  10. Arraylist.contains()
     
    Syntax
     
    boolean contains(Object o)
     
    It returns true if the list contains the specified element
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   ArrayList < String > cars = new ArrayList < String > ();  
    5.   cars.add("Ford");  
    6.   cars.add("Ferrari");  
    7.   cars.add("Maruti");  
    8.   cars.add("Vokesvagen");  
    9.   boolean ans = arr.contains("Ford");  
    10.   if (ans)  
    11.    System.out.println("The list contains Ford");  
    12.   else  
    13.    System.out.println("The list does not contain Ford");  
    14.  }  
    15. }  
    In the above code, we are checking for the presence of "Ford". If we can find it in the ArrayList cars, we print "the list contains Ford" otherwise we print "The list does not contain Ford."
     
  11. Arraylist.indexOf()
     
    Syntax
     
    int indexOf(Object o)
     
    It is used to return the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   ArrayList < String > cars = new ArrayList < String > ();  
    5.   cars.add("Ford");  
    6.   cars.add("Ferrari");  
    7.   cars.add("Maruti");  
    8.   cars.add("Vokesvagen");  
    9.   boolean ans = arr.contains("Ford");  
    10.   int element = arr.indexOf("Maruti");  
    11.   if (element != -1)  
    12.    System.out.println("the indexof of" + " Maruti is " + element);  
    13.   else  
    14.    System.out.println("Maruti is not present in" + " the list");  
    15.  }  
    16. }  
    The above code will check the least recent occurrence of the element "Maruti", and returns the integer index
     
  12. Arraylist.remove()
     
    Syntax
     
    1. E remove(int index)
     
    It is used to remove the element present at the specified position in the list.
     
    2. boolean remove(Object o)
     
    It is used to remove the first occurrence of the specified element.
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   ArrayList < String > cars = new ArrayList < String > ();  
    5.   cars.add("Ford");  
    6.   cars.add("Ferrari");  
    7.   cars.add("Maruti");  
    8.   cars.add("Vokesvagen");  
    9.   boolean ans = arr.contains("Ford");  
    10.   cars.remove(2);  
    11.   cars.remove("Ford");  
    12.  }  
    13. }  
    In the above code, first, we delete "Maruti" and "Ford" from ArrayList cars
     
  13. Arraylist.removeAll()
     
    Syntax
     
    boolean removeAll(Collection<?> c)
     
    It is used to remove all the elements from the list.
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   ArrayList < String > cars = new ArrayList < String > ();  
    5.   cars.add("Ford");  
    6.   cars.add("Ferrari");  
    7.   cars.add("Maruti");  
    8.   cars.add("Vokesvagen");  
    9.   boolean ans = arr.contains("Ford");  
    10.   cars.removeAll(cars);  
    11.  }  
    12. }  
    In the above, we will remove the whole of the ArrayList cars
     
    Note
    clear() is faster than that of remove()
     
  14. Arraylist.removeIf()
     
    Syntax
     
    boolean removeIf(Predicate<? super E> filter)

    It is used to remove all the elements from the list that satisfies the given predicate 
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   ArrayList < String > cars = new ArrayList < String > ();  
    5.   cars.add("Ford");  
    6.   cars.add("Ferrari");  
    7.   cars.add("Maruti");  
    8.   cars.add("Vokesvagen");  
    9.   boolean ans = arr.contains("Ford");  
    10.   cars.removeIf(n -> (n == " Maruti"));  
    11.  }  
    12. }  
    In the above code, we are removing all the occurrences of "Maruti" from the ArrayList cars.
                      
  15. Arraylist.removeRange()
     
    Syntax
     
    protected void removeRange(int fromIndex, int toIndex)
     
    It is used to remove all the elements lies within the given range.
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   ArrayList < String > cars = new ArrayList < String > ();  
    5.   cars.add("Ford");  
    6.   cars.add("Ferrari");  
    7.   cars.add("Maruti");  
    8.   cars.add("Vokesvagen");  
    9.   boolean ans = arr.contains("Ford");  
    10.   cars.removeRange(02)  
    11.  }  
    12. }  
    In the above code, we are removing the first 2 elements i.e "Ford" and "Ferrari"
     
  16. Arraylist.replaceAll()
     
    Syntax
     
    void replaceAll(UnaryOperator<E> operator)
     
    It is used to replace all the elements from the list with the specified element.
    1. import java.util.ArrayList;  
    2. import java.util.function.*;  
    3. class MyOperator < T > implements UnaryOperator < T > {  
    4.  T varc1;public T apply(T varc) {  
    5.   return varc1;  
    6.  }  
    7. }  
    8. public class Csharpcorner {  
    9.  public static void main(String[] args) {  
    10.   ArrayList < String > cars = new ArrayList < String > ();  
    11.   MyOperator < String > operator;  
    12.   operator = new MyOperator < > ();  
    13.   cars.add("Ford");  
    14.   cars.add("Ferrari");  
    15.   cars.add("Maruti");  
    16.   cars.add("Vokesvagen");  
    17.   operator.varc1 = "Ambassodor";  
    18.   cars.replaceAll(operator);  
    19.  }  
    20. }   
    In the above code, we replace "Ford", "Ferarri", "Maruti" and "Volkswagen" with "Ambassador" i.e. the whole of the ArrayList cars will contain only "Ambassador"
     
  17. Arraylist.retainAll()
     
    Syntax
     
    void retainAll(Collection<?> c)
     
    It is used to retain all the elements in the list that are present in the specified collection.
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   ArrayList < String > cars = new ArrayList < String > (capacity);  
    5.   cars.add("Ford");  
    6.   cars.add("Ferrari");  
    7.   cars.add("Maruti");  
    8.   cars.add("Vokesvagen");  
    9.   ArrayList < String > new_cars = new ArrayList < String > ();  
    10.   new_cars.add("Ford");  
    11.   new_cars.add("Ferari");  
    12.   new_cars.add("Mecedes");  
    13.   new_cars.add("Benz");  
    14.   new_cars.retainAll(cars);  
    15.  }  
    16. }  
  18. In the above code, we will be removing "Mercedes" and "Benz" from ArrayList new_cars as these two values are not present in ArrayList cars.
     
  19. Arraylist.set()
     
    Syntax
     
    E set(int index, E element)
     
    It is used to replace the specified element in the list, present at the specified position.
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   ArrayList < String > cars = new ArrayList < String > ();  
    5.   cars.add("Ford");  
    6.   cars.add("Ferrari");  
    7.   cars.add("Maruti");  
    8.   cars.add("Vokesvagen");  
    9.   cars.set(, "Benz");  
    10.  }  
    11. }  
    In the above code, we are replacing the 3rd element values to "Benz", i.e., "Maruti" will be replaced with "Benz".
     
  20. Arraylist.sort()
     
    Syntax
     
    void sort(Comparator<? super E> c)
     
    It is used to sort the elements of the list based on the specified comparator.
    1. import java.util.ArrayList;  
    2. import java.util.Collections.sort()  
    3. public class Csharpcorner {  
    4.  public static void main(String[] args) {  
    5.   ArrayList < String > cars = new ArrayList < String > ();  
    6.   cars.add("Ford");  
    7.   cars.add("Ferrari");  
    8.   cars.add("Maruti");  
    9.   cars.add("Vokesvagen");  
    10.   Collections.sort(cars);  
    11.  }  
    12. }  
    In above the code, we will be sorting the above list according to the ASCII value as the ArrayList is of String Collection type, i.e. "Ferrari", "Ford", "Maruti", "Volkswagen"
     
  21. Arraylist.spliterator()
     
    Syntax
     
    Spliterator<E> spliterator()
     
    It is used to create spliterator over the elements in a list.
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   ArrayList < String > cars = new ArrayList < String > ();  
    5.   cars.add("Ford");  
    6.   cars.add("Ferrari");  
    7.   cars.add("Maruti");  
    8.   cars.add("Vokesvagen");  
    9.   Spliterator < String > new_cars = cars.spliterator();  
    10.  }  
    11. }  
    In the above code, we have created a Spliterator named new_cars from the ArrayList cars.
     
  22. Arraylist.subList()
     
    Syntax
     
    List<E> subList(int fromIndex, int toIndex)
     
    It is used to fetch all the elements lies within the given range.
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   ArrayList < String > cars = new ArrayList < String > ();  
    5.   cars.add("Ford");  
    6.   cars.add("Ferrari");  
    7.   cars.add("Maruti");  
    8.   cars.add("Vokesvagen");  
    9.   Spliterator < String > new_cars = cars.spliterator();  
    10.  }  
    11. }  
    In the above code, we are creating an ArrayList new_cars which contains the values "Ferrari" and "Maruti".
     
  23. Arraylist.size()
     
    Syntax
     
    int size()
     
    It is used to return the number of elements present in the list.
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   ArrayList < String > cars = new ArrayList < String > ();  
    5.   cars.add("Ford");  
    6.   cars.add("Ferrari");  
    7.   cars.add("Maruti");  
    8.   cars.add("Vokesvagen");  
    9.   int size = cars.size();  
    10.  }  
    11. }  
    In the above code, we are finding the number of elements that are present in ArrayList cars
     
  24. Arraylist.trimToSize()
     
    Syntax
     
    void trimToSize()
     
    It is used to trim the capacity of this ArrayList instance to be the list's current size.
    1. import java.util.ArrayList;  
    2. public class Csharpcorner {  
    3.  public static void main(String[] args) {  
    4.   int capacity = 20;  
    5.   ArrayList < String > cars = new ArrayList < String > (capacity);  
    6.   cars.add("Ford");  
    7.   cars.add("Ferrari");  
    8.   cars.add("Maruti");  
    9.   cars.add("Vokesvagen");  
    10.   cars.trimToSize();  
    11.  }  
    12. }  
    In the above code, we will be reducing the size of ArrayList cars from 20 to 4 (i.e., current ArrayList size)
     

Conclusion

 
In this article, we learned about ArrayList, the difference between Array and List, features of ArrayList, ArrayList Methods, and how to use ArrayList Class and Methods using Java.
 
Next Tutorial in this Series >>Java Array