Home Tutorial Java Collections Arraylist Java arraylist of arraylist

 
 

Share on Google+Share on Google+
Java arraylist of arraylist
Posted on: October 23, 2009 at 12:00 AM
Advertisement
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 
Advertisement

Related Tags for Java arraylist of arraylist:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 23, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com