Error generic array creation
Asked Answered
O

3

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?

Osterman answered 11/10, 2010 at 2:31 Comment(0)
A
5

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

Here's the formal explanation.

Amaryllidaceous answered 11/10, 2010 at 2:38 Comment(0)
U
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.

Unskilled answered 11/10, 2010 at 2:36 Comment(0)
G
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.

Gales answered 11/10, 2010 at 2:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.