简体   繁体   中英

Error generic array creation

public class TwoBridge implements Piece{
    private HashSet<Hexagon>[] permutations;

    public TwoBridge(){
        permutations = new HashSet<Hexagon>[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<Hexagon>[6];
                       ^
1 error

How can I resolve this?

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

Here's the formal explanation .

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM