I need to store a list of unknown number of objects in a 2D array of fixed length. Example I have a 2d array:
int[][] clashesMatrix = new int[noOfExams][noOfExams];
and in each clashesMatrix position I need an arraylist of dynamic size with a Student object. I tried to do this:
ArrayList<Student>[][] clashesMatrix2 = new ArrayList<Student>[][];
but this gives a syntax error: Cannot create a generic array of ArrayList<Student>
. Is there a way in which I can accomplish my goal with this?
There is another option of course to have a 3 Dimensional ArrayList
ArrayList<ArrayList<ArrayList<Student>>> clashesMatrix = new ArrayList<ArrayList<ArrayList<Student>>>();
What would you suggest to use for this purpose? And if I use the 2nd option of 3D arraylists what is the best way to initialize all the arraylist since of course everything would be null in the beginning? Would this be an overhead to loop over and initialize all arraylists I need?
ArrayList<ArrayList<ArrayList<Student>>>
, but why are you concerned about initialisation? Can't you just delay initialisation until you need each list?List<String[]>
for example.List<List<?>>
is painful to manipulate and requires copy-pasting of loops and nullchecks around - this should be a massive red flag. Consider theArrayTable
subclass as it's blazing fast and you seem to have a fixed size anyway.