Skip to content
Related Articles

Related Articles

Improve Article

ArrayList of ArrayList in Java

  • Difficulty Level : Easy
  • Last Updated : 11 Dec, 2018

We have discussed that an array of ArrayList is not possible without warning. A better idea is to use ArrayList of ArrayList.

// Java code to demonstrate the concept of
// array of ArrayList
import java.util.*;
public class Arraylist {
    public static void main(String[] args)
        int n = 3;
        // Here aList is an ArrayList of ArrayLists
        ArrayList<ArrayList<Integer> > aList = 
                  new ArrayList<ArrayList<Integer> >(n);
        // Create n lists one by one and append to the 
        // master list (ArrayList of ArrayList)
        ArrayList<Integer> a1 = new ArrayList<Integer>();
        ArrayList<Integer> a2 = new ArrayList<Integer>();
        ArrayList<Integer> a3 = new ArrayList<Integer>();
        for (int i = 0; i < aList.size(); i++) {
            for (int j = 0; j < aList.get(i).size(); j++) {
                System.out.print(aList.get(i).get(j) + " ");
1 2 
10 20 30

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :