Lonely Twinky Lonely Twinky - 7 months ago 9
Java Question

ArrayList of Arraylits

I have to use 10 ArrayLists for a project, instead of creating 10 separate arraylists. I was told to use:

ArrayList<ArrayList<Integer>> lists = new ArrayList<>();
and to create the 10 arrays to use:
for(int i=0;i<10;i++){ lists.add(new ArrayList<Integer> ());


I have never studied arraylists inside of a arraylist, so this is very new to me. How do I access the arrays inside of the array? They do not have a name that I can see to access, so how do I call each individual arraylist?

Answer

To add a number to a list in the list of lists, you have to get it using its index, then you can add to it.

lists.get(0).add(42);