Java ArrayList

In this tutorial, we will learn about what is java.util.ArrayList and methods of it with explanation, uses cases of it along with working examples of it.

ArrayList

The java.util.ArrayList is a class in collections framework and is one of the mostly used class in the collection framework. This class can be considered as alternative for creating dynamic arrays and it is an implementation of the List interface and provides a convenient way to store, manipulate and access the data.

It is a backed by array and size of the array is dynamic that grows as the number of elements in it increases and an alternative to the traditional array which is of a fixed size.

You can add, remove, or search for elements in the list, and it automatically resizes itself based on the number of elements in the list.

It also provides additional methods like sort(), indexOf(), and toArray().

ArrayList methods

Following are the methods of ArrayList and description of each method.

MethodDescription
add(E e)Adds the specified element to the end of this list.
add(int index, E element)Inserts the specified element at the specified position in this list.
addAll(Collection<? extends E> c)Appends all 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.
addAll(int index, Collection<? extends E> c)Inserts all the elements in the specified collection into this list, starting at the specified position.
clear()Removes all the elements from this list.
clone()Returns a shallow copy of this ArrayList instance.
contains(Object o)Returns true if this list contains the specified element.
get(int index)Returns the element at the specified position in this list.
indexOf(Object o)Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
isEmpty()Returns true if this list contains no elements.
remove(Object o)Removes the first occurrence of the specified element from this list, if it is present.
remove(int index)Removes the element at the specified position in this list.
removeAll(Collection<?> c)Removes all the elements from this list that are present in the specified collection.
set(int index, E element)Replaces the element at the specified position in this list with the specified element.
size()Returns the number of elements in this list.
toArray()Returns an array containing all the elements in this list in proper sequence (from first to last element).
toString()Returns a string representation of this ArrayList instance, containing the string representation of each element in the order they are stored in the list.

ArrayList add(), remove() and contains() method example :

The following is an example of the ArrayList.

CopiedCopy Code
import java.util.ArrayList;
public class ArrayListExample {
   public static void main(String[] args) {
      // Creating an ArrayList
      ArrayList<String> vegetables = new ArrayList<String>();
      // Adding elements to the ArrayList
      vegetables.add("Onion");
      vegetables.add("Tomato");
      vegetables.add("Potato");
      // Printing the elements of the ArrayList
      System.out.println("Vegetables in the list: " + vegetables);
      // Accessing an element of the ArrayList using the get() method
      System.out.println("The first vegetable in the list is: " + vegetables.get(0));
      // Removing an element from the ArrayList
      vegetables.remove(1);
      System.out.println("Vegetables in the list after removing Tomato: " + vegetables);
      // Checking if an element exists in the ArrayList using the contains() method
      if (vegetables.contains("Tomato")) {
         System.out.println("Tomato is in the list");
      } else {
         System.out.println("Tomato is not in the list");
      }
   }
}

Output:

Vegetables in the list: [Onion, Tomato, Potato]
The first vegetable in the list is: Onion
Vegetables in the list after removing Onion: [Onion, Potato]
Tomato is not in the list

Explanation of example:

  1. In this example, we create an ArrayList of strings and add three vegetables to the list.
  2. We then print the elements of the list using the println() method.
  3. We access the first vegetable in the list using the get() method and remove the second vegetable from the list using the remove() method.
  4. We use the contains() method to check if a vegetable exists in the list.

The above example gives an idea about how to use some of the common methods of ArrayList, such as add(), remove(), get(), and contains(). With the help of these methods, we can easily manipulate the elements of the ArrayList.

Conclusion:

In this tutorial, we have covered what is java.util.ArrayList, and its methods description along with the working example of it.