Javatpoint Logo
Javatpoint Logo

Nested ArrayList in Java

In Java programming, there are numerous records systems which might be crucial for storing and manipulating data correctly. One such facts shape is the ArrayList, that is a dynamic array that can develop or lessen as desired. In a few times, we can stumble upon conditions in which we want to store lists within lists, and this is wherein nested ArrayLists come into play. In this section, we will provide an in-depth explanation of nested ArrayList in Java, alongside code examples and output motives.

What is an ArrayList?

Before diving into nested ArrayList, permit's begin through know-how the basics of an ArrayList. An ArrayList is part of Java's Collection Framework and is frequently used to shop a group of factors. It is much like an array, but it can dynamically resize itself, making it extra flexible and convenient.

We can create an ArrayList in Java by means of importing the java.util package and initializing it as follows:

In the above code, we've got created an ArrayList named myList that may shop integers. We can also store different data types like Strings, Objects, or custom training in an ArrayList.

Nested ArrayList

A nested ArrayList is an ArrayList of ArrayLists. In different phrases, it is a two-dimensional ArrayList. Each detail inside the outer ArrayList is itself an ArrayList. It shapes permits us to create a matrix-like records shape wherein we may get right of entry to elements the use of indices. Nested ArrayLists are particularly beneficial while you need to shop and control multi-dimensional information.

Creating a Nested ArrayList


0 1 2 
3 4 5 
6 7 8 

As we can see, we efficiently created a nested ArrayList representing a 3x3 matrix and revealed its contents.

Accessing Elements in a Nested ArrayList:

Accessing factors in a nested ArrayList is similar to having access to elements in a 2D array. We want indices to specify the row and column. Let's see a way to get admission to factors inside the nested ArrayList we created inside the preceding example:

In this code, we use nestedArrayList.get(1) to access the second row (remember, indexing starts from 0), and then get(2) to access the third column. The output will be:

Adding and Removing Elements in Nested ArrayList:

We can add elements to a nested ArrayList using the add() method and remove elements using the remove() method. Let's add a new row to our existing matrix:

Now, our matrix will look like this:

Now, the matrix will be without the second row:

Iterating Through Nested ArrayList:


0 1 2 
6 7 8 
9 10 11 

In conclusion, we have explored the concept of nested ArrayList in Java. We have included the way to create, manipulate, and get right of entry to elements in a nested ArrayList, along with a few not unusual use instances. Nested ArrayList are a powerful device for handling multi-dimensional facts structures in Java, imparting flexibility and ease of use in various programming scenarios. With this know-how, you can leverage nested ArrayList to clear up a wide variety of programming worrying conditions, from representing matrices to handling complex information systems.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA