I am just starting to learn Java and I'm working through a simple application that works with a deck of cards. Currently, I'm trying to instantiate a Suit class with an enum. I wanted to use the EnumSet functionality, but I'm really having trouble wrapping my head around what is going on here.
I have looked through several examples and I believe I'm just not fully comprehending the syntax.
Here's what I have so far. What I want to end up with is an EnumSet with the values of each Suit
of Cards (clubs, diamonds, hearts, spades).
public class Suits {
public enum Suit {
CLUBS("Clubs"),
DIAMONDS("Diamonds"),
HEARTS("Hearts"),
SPADES("Spades");
private String displayText;
Suit(String displayText) {
this.displayText = displayText;
}
public String getDisplayText() {
return this.displayText;
}
}
final EnumSet<Suit> allSuits = EnumSet.allof(Suit.values());
}
I know that the syntax on the final EnumSet
is wrong. I'm just not sure what exactly I am doing wrong. Any help is appreciated.
You should pass a Class<Suit>
instance instead of a Suit[]
:
final EnumSet<Suit> allSuits = EnumSet.allOf(Suit.class);
The method signature is allOf(Class<E> elementType)
.