Blog / May 1, 2023 / 4 mins read / By Suneet Agrawal

ArrayList in Java

ArrayList is one of the most commonly used data structures in Java. It provides a dynamic array implementation that allows you to add and remove elements from the list. The size of the ArrayList can also be dynamically changed based on the number of elements added or removed from the list. In this blog, we will dive into the details of ArrayList in Java, how it works, and some examples of how to use it.


What is an ArrayList in Java?

ArrayList is a class in the Java collections framework that provides a dynamic array implementation. It is similar to an array in that it stores a collection of elements, but it has several advantages over traditional arrays:

  • ArrayList can grow or shrink dynamically, unlike arrays that have a fixed size.
  • ArrayList can store any type of object, whereas arrays are restricted to a specific type.

In other words, an ArrayList is a resizable array that can store any type of object.


Features of ArrayList

  • Dynamic Size - The size of an ArrayList can be dynamically increased or decreased based on the elements that are added or removed from it.
  • Ordered Collection - An ArrayList is an ordered collection, meaning that the elements in it are stored in a specific order, and the position of each element is maintained.
  • Supports Heterogeneous Objects - Unlike traditional Java arrays, an ArrayList can store different types of objects in the same collection.
  • Allows Duplicate Elements - An ArrayList allows duplicate elements to be stored in it.
  • Random Access - Elements in an ArrayList can be accessed randomly, i.e., you can directly access any element in the ArrayList by its index number.
  • Implements Serializable and Cloneable Interfaces - An ArrayList implements the Serializable and Cloneable interfaces, which allows it to be easily serialized and cloned.

Creating an ArrayList

To create an ArrayList in Java, you need to import the java.util.ArrayList package and then create an instance of the ArrayList class using the new keyword. Here’s an example:

import java.util.ArrayList;

public class Main {
          public static void main(String[] args) {
        // create an ArrayList of strings
        ArrayList<String> fruits = new ArrayList<String>();

        // add some elements to the ArrayList
        fruits.add("apple");
        fruits.add("banana");
        fruits.add("orange");

        // print the elements of the ArrayList
        System.out.println(fruits);
    }
}

In this example, we create an ArrayList of strings called fruits. We then add three elements to the ArrayList using the add method and print the elements using the println method.


Accessing Elements in an ArrayList

You can access elements in an ArrayList using the get() method, which takes an index as its argument and returns the element at that index. Here’s an example:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        // create an ArrayList of strings
        ArrayList<String> fruits = new ArrayList<String>();

        // add some elements to the ArrayList
        fruits.add("apple");
        fruits.add("banana");
        fruits.add("orange");

        // get the element at index 1
        String secondFruit = fruits.get(1);

        // print the element
        System.out.println(secondFruit);
    }
}

In this example, we use the get method to retrieve the element at index 1 (which is banana) and assign it to a variable called secondFruit. We then print the value of secondFruit.


Adding and Removing Elements in ArrayList

You can add and remove elements from an ArrayList using the add() and remove() methods. Here is an example of how to add and remove elements:

import java.util.ArrayList;

public class ArrayListExample {
   public static void main(String[] args) {
      ArrayList<String> list = new ArrayList<String>();
      list.add("Apple");
      list.add("Banana");
      list.add("Orange");
      
      list.add("Grapes");
      System.out.println(list);
      
      list.remove(2);
      System.out.println(list);
   }
}

The above code will add Grapes to the end of the ArrayList and print the updated list. It will then remove the element at index 2 (which is Orange) and print the updated list again.


Modifying Elements in an ArrayList

You can modify elements in an ArrayList using the set() method, which takes an index and a new value as its arguments and replaces the element at the specified index with the new value. Here’s an example:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        // create an ArrayList of strings
        ArrayList<String> fruits = new ArrayList<String>();

        // add some elements to the ArrayList
        fruits.add("apple");
        fruits.add("banana");
        fruits.add("orange");

        // set the element at index 1 to "pear"
        fruits.set(1, "pear");

        // print the modified ArrayList
        System.out.println(fruits);
    }
}

In this example, we use the set method to replace the element at index 1 (which is banana) with the value pear. We then print the modified ArrayList.


ArrayList Size

The size() method is used to get the number of elements in the ArrayList. Here is an example:

import java.util.ArrayList;

public class ArrayListExample {
   public static void main(String[] args) {
      ArrayList<String> list = new ArrayList<String>();
      list.add("Apple");
      list.add("Banana");
      list.add("Orange");
      
      int size = list.size();
      System.out.println(size);
   }
}

The above code will print 3 because there are three elements in the ArrayList.


Comments