oadams
oadams

Reputation: 3087

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?

Upvotes: 2

Views: 5438

Answers (3)

biasedbit
biasedbit

Reputation: 2870

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

Here's the formal explanation.

Upvotes: 5

Ibrahim
Ibrahim

Reputation: 1261

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.

Upvotes: 0

C. K. Young
C. K. Young

Reputation: 223193

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.

Upvotes: 2

Related Questions