nested lists in java code example

Example 1: Lists inside lists in java

public class GuiTest {

	
	public static void main(String[] args) {
		List> MainList = new ArrayList>();
		Random NewRandomNumber = new Random();
		
		for (int i = 0; i < 10; i++) {
			List SecondList = new ArrayList();
			MainList.add(SecondList);
			for (int i2 = 0; i2 < 10; i2++) {
				SecondList.add(NewRandomNumber.nextInt(6));
			}
		}

Example 2: arraylist of arraylist

public static void main(String[] args) {
    ArrayList> outer = new ArrayList>();
    ArrayList inner = new ArrayList();        

    inner.add(100);     
    inner.add(200);
    outer.add(inner); // add first list
    inner = new ArrayList(inner); // create a new inner list that has the same content as  
                                           // the original inner list
    outer.add(inner); // add second list

    outer.get(0).add(300); // changes only the first inner list

    System.out.println(outer);
}

Tags:

Misc Example