Sunday, January 29, 2017

List of lists in Java


package com.test;
import java.util.ArrayList;
public class Arraylist {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<ArrayList> x = new ArrayList<>();
ArrayList al1 = new ArrayList(); //first array list
al1.add("banana"); //add three fruits to first array list
al1.add("starwbeery");
al1.add("apple");
//Now add first array list to x which is array of array list
x.add(al1);
ArrayList al2 = new ArrayList(); //second array list
al2.add("carrom");//add three games to second array list
al2.add("chess");
al2.add("badminton");
//Now add second array list to x which is array of array list
x.add(al2);
ArrayList al3 = new ArrayList();//third array list
al3.add("Gangully"); //add three players to thirs array list
al3.add("Zaheer");
al3.add("Dravid");
//Now add third array list to x which is array of array list
x.add(al3);
//To get these elements
for(ArrayList al: x){
System.out.println(al); //This prints the three different array lists
//To get each element from each arraylist
for(Object element : al){
System.out.println(element);
}
}
}
}