how to create list of list in java code example
Example 1: how to create a list in java
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
class scratch{
public static void main(String[] args) {
List<Integer> aList = new ArrayList<>();
List<Integer> lList = new LinkedList<>();
}
}
Example 2: Lists inside lists in java
public class GuiTest {
public static void main(String[] args) {
List<List<Integer>> MainList = new ArrayList<List<Integer>>();
Random NewRandomNumber = new Random();
for (int i = 0; i < 10; i++) {
List<Integer> SecondList = new ArrayList<Integer>();
MainList.add(SecondList);
for (int i2 = 0; i2 < 10; i2++) {
SecondList.add(NewRandomNumber.nextInt(6));
}
}
Example 3: how to make java list
ArrayList<String> list = new ArrayList<String>();
list.add("Mango");
list.add("Banana");
list.set(1,"Dates");
System.out.println("Returning element: " + list.get(1));
Example 4: arraylist of arraylist
public static void main(String[] args) {
ArrayList<ArrayList<Integer>> outer = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> inner = new ArrayList<Integer>();
inner.add(100);
inner.add(200);
outer.add(inner);
inner = new ArrayList<Integer>(inner);
outer.add(inner);
outer.get(0).add(300);
System.out.println(outer);
}