I know that you cannot create an array of a generic type, Instead you have to resort to a hack. (Given Java supports generic arrays, just not their creation, it is not clear to
What you need to realize is that your situation is the same as the first situation you described.
Inner
is a non-static inner class of Outer
, a generic class. That means Inner
is within the scope of the type parameter, and simply writing Inner
is short for Outer
. i.e. it may not look it, but simply Inner
is a parameterized type, just like Map.Entry
, because the type parameter E
of the outer class becomes implicitly a type parameter of the inner class. The solution to both problems is the same.
Your solution to the first problem was to create an array of the raw type, i.e. new Map.Entry[numEntries];
. What is the raw type here? Not Inner
, as we already discussed. Instead, you need to explicitly qualify the outer type to access the raw type: new Outer.Inner[16];
. Of course, you need a cast to cast it back into the desired generic array type:
(Inner[])new Outer.Inner[16]
There is another way to create an array of a generic type, without using a raw type -- using a wildcarded type, i.e. new Map.Entry, ?>[numEntries];
. The equivalent for our case would be new Outer>.Inner[16];
. With the cast:
(Inner[])new Outer>.Inner[16]