Error generic array creation

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

public class TwoBridge implements Piece{     private HashSet[] permutations;      public TwoBridge(){         permutations = new HashSet[6]; 

Hi, I'm trying to create an array of Sets of hexagons (hexagons being a class i created).

However I get this error when I try to compile

oliver@oliver-desktop:~/uni/16/partB$ javac oadams_atroche/TwoBridge.java  oadams_atroche/TwoBridge.java:10: generic array creation         permutations = new HashSet[6];                        ^ 1 error 

How can I resolve this?

回答1:

You can't create arrays with generics. Use a Collection> or (Array)List> instead.

Here's the formal explanation.



回答2:

You cannot. The best you can do is make an ArrayList>.

If you are willing to deal with raw types (which are heavily discouraged), you can make an array of Set (as opposed to Set, which is not allowed). But you didn't hear this from me.



回答3:

Following will give you a warning: permutations = new HashSet[6];

However, I agree with Chris that it is better to use ArrayList instead of ordinary array.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!