0

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?

7
  • Structures like this a almost always a sign of object-phobia. For this particular problem I would probably go for a Table<Integer, Integer. List<Student>> from the Guava libraries. Otherwise write some objects to encapsulate your structure. Commented Apr 5, 2014 at 11:49
  • I'd use the ArrayList<ArrayList<ArrayList<Student>>>, but why are you concerned about initialisation? Can't you just delay initialisation until you need each list? Commented Apr 5, 2014 at 11:49
  • @BoristheSpider Thanks for the link. I actually am already using Guava library for other purposes. I just didn't come across Table. I think I'll give it a try. @ DavidWallace why would you choose arraylists though? Is it just because it is not a good idea to mix dynamic and fixes structures? About initialisation I guess you're right, shouldn't make a difference. Commented Apr 5, 2014 at 11:54
  • 1
    @Bernice as you noticed, generics and arrays don't mix. It's a bit like oil and water - you can, of course, force them to mix but it's messy. I would avoid arrays of generics type if possible. Generic types of arrays are, however, fine - List<String[]> for example. Commented Apr 5, 2014 at 11:59
  • 1
    That's exactly my point - Guava have encapsulated a structure and given an interface to access it - that is what OOP is all about. A List<List<?>> is painful to manipulate and requires copy-pasting of loops and nullchecks around - this should be a massive red flag. Consider the ArrayTable subclass as it's blazing fast and you seem to have a fixed size anyway. Commented Apr 5, 2014 at 12:11

1 Answer 1

0

You could try this but you'll get a compiler warning

ArrayList<Student>[][] clashesMatrix2 = (ArrayList<Student>[][]) java.lang.reflect.Array.newInstance(ArrayList.class,noOfExams,noOfExams);

You could do this to suppress warning

@SuppressWarnings("unchecked")
ArrayList<Student>[][] clashesMatrix2 = (ArrayList<Student>[][]) java.lang.reflect.Array.newInstance(ArrayList.class,noOfExams,noOfExams);

More info about this can be found at http://www.ibm.com/developerworks/java/library/j-jtp01255/index.html

1
  • Thanks for your answer. I was aware of that yes but as @BoristheSpider pointed out, this is not very recommended. Instead I went for the Guava library HashBasedTable. Commented Apr 5, 2014 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.