Nested ArrayList in Java
In Java, an Arraylist is a data structure where we can store or remove elements dynamically. The arraylist is a list of arrays whose size is managed automatically as we insert or delete elements; thus, it is dynamic.
A Nested ArrayList is a dynamic list of lists, sometimes called a Multidimensional ArrayList. In other words, a nested arraylist is a list where every value of the outer List consists of an inner list.
Need of Nested ArrayList
In arrays, we had a fixed size of rows, which is not the case in a Nested ArrayList as the ArrayList is resizable. Hence, if we need to use a data structure to create n number of objects dynamically in the row, then we should utilize the nested arraylist in Java.
Syntax
ArrayList<ArrayList<Data-type>> nestedList = new ArrayList<ArrayList<Data-type>>()
- Each value of the outer ArrayList depicts the row.
- Each value of the inner ArrayList depicts the column.
- The data type indicates the data type stored in the arraylist, such as Integer and String.
Illustration
Nested ArrayList: [[31, 23,121], [2, 8, 9, 25,28], [1, 65], [21,233,43], [3]]
Example 1
In the example below, we created three ArrayLists named list1, list2 and list3. These 3 ArrayLists are then added into a nested ArrayList named nestedList, thus successfully implementing Nested ArrayList in Java.
FileName: NestedArrayList.java
import java.util.*;
public class NestedArrayList {
// main method
public static void main(String args[]) {
// Creating a Nested arraylist
ArrayList<ArrayList<Integer>> nestedList = new ArrayList<>();
// Individual ArrayList
ArrayList<Integer> list1 = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
ArrayList<Integer> list3 = new ArrayList<>();
// Insert elements in list1
list1.add(21);
list1.add(22);
list1.add(23);
// Insert elements in list2
list2.add(24);
list2.add(25);
list2.add(26);
// Insert elements in list3
list3.add(27);
list3.add(28);
list3.add(29);
// Add all the individual lists in the nested arraylist
nestedList.add(list1);
nestedList.add(list2);
nestedList.add(list3);
// Display the nested arraylist
for (ArrayList a : nestedList) {
for (Integer num : (ArrayList<Integer>) a) {
System.out.print(num + " ");
}
System.out.println();
}
}
}
Output:
21 22 23
24 25 26
27 28 29
Example 2
Now, go through another example of a Nested ArrayList given below.
FileName: NestedArrayList1.java
import java.util.*;
public class NestedArrayList1 {
// main method
public static void main(String args[]) {
// Nested arraylist
ArrayList<ArrayList<String>> groceryItems = new ArrayList<>();
// Individual arraylist: 1
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Mango");
fruits.add("Banana");
fruits.add("Strawberry");
fruits.add("Pineapple");
// Individual arraylist: 2
ArrayList<String> vegetables = new ArrayList<>();
vegetables.add("Capsicum");
vegetables.add("Brinjal");
vegetables.add("Apple gourd");
// Individual arraylist: 3
ArrayList<String> snacks = new ArrayList<>();
snacks.add("Chips");
snacks.add("Nuts");
snacks.add("Popcorn");
snacks.add("Pretzels");
// Add individual lists into nested List
groceryItems.add(fruits);
groceryItems.add(vegetables);
groceryItems.add(snacks);
// Prints the whole nested List
System.out.println("Nested ArrayList: " +groceryItems);
// Prints the elements from the first individual List
System.out.println("First individual ArrayList: " +groceryItems.get(0));
// Prints the 3rd element from 1st individual list
System.out.println("The third element from first individual list: " +groceryItems.get(0).get(2));
// Prints the 2nd element from the 3rd individual list
System.out.println("The second element from third individual list: " +groceryItems.get(2).get(1));
}
}
Output:
Nested ArrayList: [[Mango, Banana, Strawberry, Pineapple], [Capsicum, Brinjal, Apple gourd], [Chips, Nuts, Popcorn, Pretzels]]
First individual ArrayList: [Mango, Banana, Strawberry, Pineapple]
The third element from the first individual List: Strawberry
The second element from the third individual List: Nuts
Example 3
In the below example, we have implemented another example of Nested ArrayList along with a tabular representation of the same example. Note that each row can have any number of elements, and it can resized dynamically.
- | Col 0 | Col 1 | Col 2 | Col 3 | Col 3 | Col 4 | Col 5 | Col 6 |
Row 0 | 49 | - | - | - | - | - | - | - |
Row 1 | 11 | 23 | 33 | 45 | 77 | 11 | 32 | 122 |
Row 2 | 211 | 344 | 678 | 111 | - | - | - | - |
Row 3 | 1 | 88 | - | - | - | - | - | - |
Row 4 | 2211 | 234 | 1 | 2 | 3 | - | - | - |
.
FileName: NestedArrayList2.java
import java.util.*;
public class NestedArrayList2 {
// creates a Nested ArrayList
static List createNestedArrayList()
{
// Creates a Nested ArrayList of Integer type
ArrayList<ArrayList<Integer> > nl = new ArrayList<>();
// Creates first-row
nl.add(new ArrayList<>());
// Adding 49 to Row 0
nl.get(0).add(0, 49);
// Creates Row 1 and adds elements in that row
nl.add(new ArrayList<>(Arrays.asList(11, 23, 45,77)));
// Adds 33 to (Row 1, Col 2)
nl.get(1).add(2, 33);
// Adds 122 to (Row 1, Col 5)
nl.get(1).add(5, 122);
// Creates Row 2 and add elements in that row
nl.add(2, new ArrayList<>(Arrays.asList(211, 344,678,111)));
// Creates Row 3 and add elements in that row
nl.add(new ArrayList<>(Arrays.asList(1, 88)));
// Creates Row 4 and add elements in that row
nl.add(new ArrayList<>(List.of(2211,234)));
// Append the elements in Row 4
nl.get(4).addAll(Arrays.asList(1, 2, 3));
// Append elements to Row 1 starting from Col 5
nl.get(1).addAll(5, Arrays.asList(11, 32));
// returns the nested arrayList
return nl;
}
// main method
public static void main(String args[])
{
System.out.println("Nested ArrayList :");
//Display the nested arraylist using the Function call
System.out.println(createNestedArrayList());
}
}
Output:
Nested ArrayList :
[[49], [11, 23, 33, 45, 77, 11, 32, 122], [211, 344, 678, 111], [1, 88], [2211, 234, 1, 2, 3]]