I have a reference to the type class Class<?>of the generic type Element and I want to create a two dimensional array Element[][] where the second dimension null. Is there a way to do this?
The code I have right now is:
Class<?> type = ...
int length = ...
Element[][] array = (Element[][]) Array.newInstance(type, length, 0);
for (int i=0; i < array.length; i++)
array[i] = null;
Is there any way to do this without the loop and the extra allocated memory?
Edit ----
The second dimension being null means that its just an array of null references to arrays of Element. I am doing this so I can dynamically allocate the length of the second domension so that the arrays do not need to be the same length. So I can do something like this:
array[0] = (Element[]) Array.newInstance(type, 4);
array[1] = (Element[]) Array.newInstance(type, 1);
array[2] = (Element[]) Array.newInstance(type, 3);
array[3] = null;
array[4] = null;
array[5] = (Element[]) Array.newInstance(type, 8);
etc..
array[i] = null? Maybe you want to put null in each element of the 2nd dimension? What doesClass<?>have to do with this? Your question is unclear.Element[][] array = new Element[length][];