I'd like to initialize a nested list without using a for-loop, the root list which is: cakeList will contain another ones eg.(100).
My code:
1. ArrayList<ArrayList<Integer>> cakeList = new ArrayList<>();
2. for (int i=0;i<100;i++) cakeList.add(new ArrayList<>());
I've tried:
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) {
ArrayList<ArrayList<Integer>> cakeList = new ArrayList<>(100);
cakeList.forEach(e-> new ArrayList<>());
System.out.println(cakeList.size());
cakeList.get(0);
}
}
but as you maybe know, it complies, but throws an error al line #7 because cakeList is empty whe I try to cakeList.get(0).
IndexOutOfBoundsException: Index: 0, Size: 0
Thank you in advance!