Java doesn't allow arrays of inner classes for a generic class

前端 未结 3 1573
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-07 12:04

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

3条回答
  •  轮回少年
    2021-02-07 12:09

    Do the following:

    @SuppressWarnings("unchecked")
    final Inner[] inners = (Inner[])new Outer.Inner[16];
    

    The equivalent to your first example would have been new Outer.Inner[16] but this will isolate the unchecked cast and avoid the raw type.

提交回复
热议问题