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?

oadams
  • 2,929
  • 5
  • 26
  • 48

3 Answers3

5

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

Here's the formal explanation.

biasedbit
  • 2,670
  • 4
  • 25
  • 40
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.

Chris Jester-Young
  • 206,112
  • 44
  • 370
  • 418
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.

Ibrahim
  • 1,217
  • 2
  • 12
  • 21