Creating a common array of errors

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

Hi, I am trying to create an array of sets of hexagons (hexagons is the 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 solve this?

+2
java
source share
3 answers

You cannot create arrays using generics. Instead, use Collection<Set<Hexagon>> or (Array)List<Set<Hexagon>> .

Here is the official explanation .

+5
source share

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

If you are willing to deal with raw types (which are very discouraged), you can create an array of Set (unlike Set<Hexagon> , which is not valid). But you have not heard that.

+2
source share

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

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

0
source share

All Articles