Home Tutorial Java Collections Arraylist Java arraylist of arraylist

 
 

Java arraylist of arraylist
Posted on: October 23, 2009 at 12:00 AM
This section demonstrates how to make Java ArrayList of the ArrayList

  • Java arrayList can make the use of other arrayList. 
  • In one arrayList other arrayList can be added as the object 
  • It works as two dimension array.


Example of Java Arraylist of Arraylist
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class List1 {
    public static void main(String[] args) {
       ArrayList list=new ArrayList();
       ArrayList list1=new ArrayList();
       for(int x=0;x<=2;x++)
           list1.add((int)(Math.random()*15));

       ArrayList list2=new ArrayList();
       for(int x=0;x<=2;x++)
           list2.add((int)(Math.random()*15));

       ArrayList list3=new ArrayList();
       for(int x=0;x<=2;x++)
           list3.add((int)(Math.random()*15));

       list.add(list1);
       list.add(list2);
       list.add(list3);
       System.out.println(list);
        for (int i = 0; i < list.size(); i++) {            
           for (int j = 0; j < (list.get(i)).size(); j++) {
              System.out.print((list.get(i)).get(j)+"\t");
          }
       System.out.print("\n");
    }
  }
}

Output
[[14, 8, 7], [0, 7, 11], [14, 9, 14]]

14   8   7 
0    7   11 
14   9   14 

Related Tags for Java arraylist of arraylist:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.