Java

How to Create a Multi-Dimensional ArrayList in Java?

In Java, an ArrayList is a (dynamic) data structure that stores a collection of objects. A multi-dimensional ArrayList is a list where each element of the outer ArrayList contains an inner ArrayList. This allows for the creation of a two-dimensional or multi-dimensional data structure that can store a collection of objects.
This guide will illustrate the method to create a multi-dimensional ArrayList in Java.

How to Create a Multi-Dimensional ArrayList in Java?

To create a multi-dimensional ArrayList in Java, users need to first declare the “ArrayList” with the appropriate number of dimensions. Users can do this by creating an ArrayList of ArrayLists of the desired data type.

To add an element to the multi-dimensional ArrayList, use the “add()” method of the inner ArrayLists. To retrieve an element from the multi-dimensional ArrayList, use the “get()” method of the inner ArrayLists.

Syntax
The syntax to create a multi-dimensional ArrayList in Java is as follows:

ArrayList<ArrayList<DataType>> arrayList2D = new ArrayList<ArrayList<DataType>>();

The description of the above syntax is enlisted below:

  • The “DataType” represents the type of data that will be stored in the ArrayList.
  • The “arrayList2D” is the name of the two-dimensional ArrayList variable.
  • Each element of the outer “ArrayList” represents a row, and each element of the inner “ArrayList” represents a column.

To create a 3-dimensional ArrayList, users can use the following syntax:

ArrayList<ArrayList<ArrayList<DataType>>> arrayList3D = new ArrayList<ArrayList<ArrayList<DataType>>>();
  • The “arrayList3D” is the name of the three-dimensional ArrayList variable.
  • The above syntax declares and initializes a three-dimensional ArrayList in Java.

Note: Similarly, for creating an n dimensional ArrayList, users can nest ArrayLists within ArrayLists n times.

Example
Here is a basic example of how to create a two-dimensional ArrayList in Java:

import java.util.ArrayList;

public class MultiDimensionalArrayListExample {
    public static void main(String[] args) {
        // Create a two-dimensional ArrayList of integers
        ArrayList<ArrayList> twoDArrayList = new ArrayList<ArrayList>();
       
        // Create the first row and add it to the outer ArrayList
        ArrayList row1 = new ArrayList();
        row1.add(1);
        row1.add(2);
        row1.add(3);
        twoDArrayList.add(row1);
       
        // Create the second row and add it to the outer ArrayList
        ArrayList row2 = new ArrayList();
        row2.add(4);
        row2.add(5);
        row2.add(6);
        twoDArrayList.add(row2);
       
        // Retrieve and print the value at the second row and the third column
        int value = twoDArrayList.get(1).get(2);
        System.out.println("Value at row 2, column 3: " + value);
    }
}

The explanation of the above code is given below:

  • First, create a two-dimensional ArrayList of integers called “twoDArrayList”.
  • Then, create two inner “ArrayLists” (row1 and row2) representing the rows of the two-dimensional ArrayList and add them to the outer “twoDArrayList” using the “add()” method.
  • Finally, retrieve and print the value at the second row and third column using the get() method of the inner ArrayLists.

Output

This example creates a two-dimensional ArrayList with hard-coded values.

Note: Users can easily modify it to add elements dynamically based on user input or other conditions.

Conclusion

To create a multi-dimensional ArrayList in Java, first declare an ArrayList of ArrayLists with the desired number of dimensions and appropriate data type. Then, add and retrieve elements using the “add()” and “get()” methods of the inner ArrayLists. In addition, users can create a multi-dimensional ArrayList with more than two dimensions by nesting ArrayLists within ArrayLists. This article has explained the detailed explanation to create a multi-dimensional ArrayList in Java.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.