How do I make a multidimensional array of generic items in java?
Consider the class:
class A<T>
{
T t;
public A(T t) { this.t = t; }
}
When I try to create a multidimensional array:
A<String>[][] array = new A<String>[2][3];
I get the following error:
generic array creation
A<String>[][] array = new A<String>[2][3];
^
I tried the following:
A<String>[][] array = (A<String>[][]) (new Object[2]3]);
But that just throws: java.lang.ClassCastException
What's the fix?
(I anticipate people recommending to use lists. Please explain how to achieve this using arrays.)
array[i][j]
is more legible thanarray.get(i).get(j)
. When you're coding a complex algorithm, this is really helpful. – dsg Mar 26 '11 at 22:01