What are Arrays of ArrayList in Java?

The ArrayList in java is a class of resizable array objects of various data types. It enables operations requiring memory utilisation to be carried out efficiently. It has a bundle of built-in functions which help create and delete memory when adding or removing objects from the ArrayList. This makes it dynamic in nature, similar to vectors in C++. We will now look at some syntax for creating an ArrayList.

To execute the code in this article, you must have Java compiler installed on your device.

Creating an ArrayList in Java

To start, we will import the java.util.ArrayList package and create an ArrayList of type string using the syntax shown below:

import java.util.ArrayList;
class Main
{
  public static void main(String[] args){
    // create ArrayList
    ArrayList<String> cars = new ArrayList<>();
  }
}

Explanation

  • Line#1: We import the java.util.ArrayList package first.
  • Line#3: The main class is defined where our code will be executed.
  • Line#5: We define the main method where code execution starts.
  • Line#8: The ArrayList cars is created of a type string.

ArrayList in Java Built-in Functions

ArrayList has multiple pre-build functions to play with dynamic arrays in java. These functions can add, access, change, or remove elements from ArrayList class arrays. Let’s discuss some functions which can help during problem-solving.

Add Element

We can add elements by using the following function:

cars.add("Honda City");

The Honda City element is added to the 0 index of the cars ArrayList.

Access Element 

In order to access elements, copy the following code:

cars.get(0)

This returns Honda City  previously added to the cars ArrayList.

Set Element

Use the following syntax to overwrite the first element in the ArrayList.

cars.set(0, "Honda Civic");

The first value is the index followed by the new element.

Remove One Element

The following line of code helps to remove an element from the ArrayList.

cars.remove(1);

The element at the first index of the cars ArrayList is removed.

Remove All Elements

The following syntax helps clear the ArrayList.

cars.clear();

The cars ArrayList is cleared removing all elements.

Coding Implementation of ArrayList

The functionality of the operations explained above has been shown using the following coding example.

import java.util.ArrayList;
 
class Main
{
  public static void main(String[] args){
 
    // create ArrayList
    ArrayList<String> cars = new ArrayList<>();
 
    // Add Elements
    cars.add("Honda City");
    cars.add("Toyota Corolla");
 
    // Set Elements
    cars.set(0, "Honda Civic");
   
    // Access Element
    System.out.println("Car at index 0: " + cars.get(0));
   
    System.out.println("All cars: " + cars);
 
    // Remove One Element
    String str = cars.remove(1);
    System.out.println("Car removed:" + str );
 
    System.out.println("All cars: " + cars);
 
    //Remove All Elements
    cars.clear();
 
    System.out.println("All cars: " + cars);
   
  }
}

Output

ArrayList in Java
ArrayList in Java

Iterating an ArrayList Using for-each Loop

An ArrayList can be iterated using the for-each loop as shown in the syntax below.

for (String strs : cars) {
 System.out.print(strs);
 System.out.print(" ");
}

The strs item of type string will be assigned the value contained at each index of the ArrayList when it is iterated using the for-each loop. This value is then printed on the terminal using the System.out.print() command.

Stay in the Loop

Get the daily email from Algoideas that makes reading the news actually enjoyable. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

- Advertisement -

You might also like...