How does one instantiate an array of maps in Java?

后端 未结 8 1238
刺人心
刺人心 2020-11-30 06:34

I can declare an array of maps using generics to specify the map type:

private Map[] myMaps;

However, I can\'t figur

8条回答
  •  無奈伤痛
    2020-11-30 07:08

    Short answer appears to be that you really just can't.

    See the following for a blog about it. http://www.bloggingaboutjava.org/2006/01/java-generics-quirks/

    One of the comments to the blog states that:

    Actually, the engineers made the creation of such an Array illegal. So the creation of an array from generic Class fails. The Collection.toArray method followed by a Cast to the Array works at compile time.

    This solves not the problem, that the ArrayStoreCheck can’t be done during Runtime, but you can create an Array of generics in this way.

    As suggested by Bill the Lizard, you probably are better off using a

    List>
    

提交回复
热议问题