ArrayList Java Tutorial [With Examples]

Introduction

In this tutorial, we will learn about ArrayList Java. With the use of examples, we will learn about the ArrayList’s different operations and methods.

  • In Java, we use the ArrayList class to implement the functionality of resizable arrays.
  • It implements the List interface of the collections framework.

How to Create ArrayList java

Before we can use ArrayList, we must first import the java.util.ArrayList package. Here’s how to create ArrayList in Java:

ArrayList<Type> arrayList= new ArrayList<>();

Here, Type indicates the type of an ArrayList.

// create Integer type arraylist
ArrayList<Integer> arrayList = new ArrayList<>();

// create String type arraylist
ArrayList<String> arrayList = new ArrayList<>();

In the above program, we have used Integer not int. It is because we cannot utilize primitive types while generating an ArrayList. Instead, we must use the appropriate wrapper classes.

Code 01: Below snippet shows you how to create Java ArrayList.

// welcome to softhunt.net
import java.util.ArrayList;

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

    // create ArrayList
    ArrayList<String> Websites = new ArrayList<>();

    // Add elements to ArrayList
    Websites.add("Softhunt.net");
    Websites.add("Pythonprogramming.com");
    Websites.add("Tutorial.io");
    System.out.println("ArrayList: " + Websites);
  }
}

Output:

ArrayList: [Softhunt.net, Pythonprogramming.com, Tutorial.io]

Basic Operations

The ArrayList class contains a number of methods for performing various operations on ArrayList. In this section, we will look at several often used ArrayList operations

  1. Add elements
  2. Access elements
  3. Change elements
  4. Remove elements

Operation 01: Add items to ArrayList

Code 02: To add a single member to the ArrayList, we utilize the ArrayList class’s add() function.

import java.util.ArrayList;

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

    // create ArrayList
    ArrayList<String> Websites = new ArrayList<>();

    // add() method without the index parameter
    Websites.add("Softhunt.net");
    Websites.add("Pythonprogramming.com");
    Websites.add("Tutorial.io");
    System.out.println("ArrayList: " + Websites);
  }
}

Output:

ArrayList: [Softhunt.net, Pythonprogramming.com, Tutorial.io]

Code 03: Add an element at a specified position in an ArrayList

import java.util.ArrayList;

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

    // create ArrayList
    ArrayList<String> Websites = new ArrayList<>();

    // add() method without the index parameter
    Websites.add("Softhunt.net");
    Websites.add("Pythonprogramming.com");
    Websites.add("Tutorial.io");
    
    // add JavaScript.com at index 1
    Websites.add(0, "JavaScript.com");

    // add C++.net at index 3
    Websites.add(4, "C++.net");
    System.out.println("ArrayList: " + Websites);
  }
}

Output:

ArrayList: [JavaScript.com, Softhunt.net, Pythonprogramming.com, Tutorial.io, C++.net]

Code 04: Add all elements of a set to an arraylist. We can also add all elements of a collection (set, map) to an ArrayList using the addAll() method.

import java.util.ArrayList;
import java.util.HashSet;

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

    // create a set
    HashSet<String> vowels = new HashSet<>();
    vowels.add("a");
    vowels.add("e");
    vowels.add("i");
    vowels.add("o");
    vowels.add("u");

    // create an arraylist
    ArrayList<String> alphabets = new ArrayList<>();

    // add all elements of set to arraylist
    alphabets.addAll(vowels);
    System.out.println("ArrayList: " + alphabets);
  }
}

Output:

ArrayList: [a, e, u, i, o]

Operation 02: Access items

Code 05: The get() function of the ArrayList class is used to access an element from the ArrayList.

import java.util.ArrayList;

class Main {
  public static void main(String[] args) {
    ArrayList<String> animals = new ArrayList<>();

    // add elements in the arraylist
    animals.add("Peacock");
    animals.add("Markhor");
    animals.add("Cow");
    System.out.println("ArrayList: " + animals);

    // get the element from the arraylist
    String str = animals.get(1);
    System.out.print("Element at index 1: " + str);
  }
}

Output:

ArrayList: [Peacock, Markhor, Cow]
Element at index 1: Markhor

Operation 03: Change ArrayList Elements

Code 06: The set() function of the ArrayList class is used to change the items of the ArrayList.

import java.util.ArrayList;

class Main {
  public static void main(String[] args) {
    ArrayList<String> animals = new ArrayList<>();

    // add elements in the arraylist
    animals.add("Peacock");
    animals.add("Markhor");
    animals.add("Cow");
    System.out.println("ArrayList: " + animals);

    // change the element of the array list
    animals.set(2, "Dog");
    System.out.println("Modified ArrayList: " + animals);
  }
}

Output:

ArrayList: [Peacock, Markhor, Cow]
Modified ArrayList: [Peacock, Markhor, Dog]

Operation 04: Remove ArrayList Elements

Code 07: We may utilize the ArrayList class’s remove() function to remove an element from the ArrayList.

import java.util.ArrayList;

class Main {
  public static void main(String[] args) {
    ArrayList<String> animals = new ArrayList<>();

    // add elements in the arraylist
    animals.add("Peacock");
    animals.add("Markhor");
    animals.add("Cow");
    System.out.println("ArrayList: " + animals +"\n");

    // remove element from index 2
    String str = animals.remove(2);
    System.out.println("Updated ArrayList: " + animals);
    System.out.println("Removed Element: " + str);
  }
}

Output:

ArrayList: [Peacock, Markhor, Cow]
Updated ArrayList: [Peacock, Markhor]
Removed Element: Cow

ArrayList Java Methods

MethodDescription
size()Returns the length of the ArrayList.
sort()Sort the ArrayList elements.
clone()Creates a new ArrayList with the same element, size, and capacity.
contains()Searches the ArrayList for the specified element and returns a boolean result.
ensureCapacity()Specifies the total element the ArrayList can contain.
isEmpty()Check if the ArrayList is empty.
indexOf()Searches a specified element in an ArrayList and returns the index of the element.

iterate Through ArrayList Java

To loop through each element of the ArrayList, we may use the Java for-each loop.

Code 08:

import java.util.ArrayList;

class Main {
  public static void main(String[] args) {
    ArrayList<String> animals = new ArrayList<>();

    // add elements in the arraylist
    animals.add("Peacock");
    animals.add("Markhor");
    animals.add("Cow");
    System.out.println("ArrayList: " + animals +"\n");

// iterate using for-each loop
    System.out.println("Accessing individual elements:  \n");
    for (String language : animals) {
      System.out.print(language);
      System.out.print("\n");
    }
  }
}

Output:

ArrayList: [Peacock, Markhor, Cow]
Accessing individual elements:  

Peacock
Markhor
Cow

FAQs

Array vs ArrayList Java

Before we can use an array in Java, we must declare its size. It is difficult to adjust the size of an array once it has been declared. We may utilize the ArrayList class to solve this problem. It enables us to make resizable arrays.

ArrayList, unlike arrays, may automatically change their capacity as we add or delete elements from them. As a result, ArrayLists are often referred to as dynamic arrays.

LinkedList vs ArrayList java

LinkedListArrayList
Implements List, Queue, and Deque interfaces.Implements List interface
Stores 3 values: data, previous and next addressStores a single value.
Provides the functionality of a doubly-linked listProvides the functionality of a resizable array.

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. How to Initialize an Array in Java – [With Examples]

Leave a Comment