2
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?

3 Answers 3

5

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

Here's the formal explanation.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.