我试图使用for循环逐个获取枚举值,而无需指定所有可能的值,例如我在代码Value.As
中执行的操作将替换为{ {1}}但是当我尝试这个时它并不适合我:
Value.values()
答案 0 :(得分:3)
您可以在values()
上使用for-each
loop。对于每个套装,读取每个套装,为每个值添加一张带有套装的新卡片和值List
。此外,您可以使用菱形运算符<>
,
public Deck(int nbBox) {
this.cardList = new LinkedList<>(); // <-- diamond operator
for (int i = 0; i < nbBox; i++) {
for (Suit suit : Suit.values()) {
for (Value value : Value.values()) {
cardList.add(new Card(suit, value));
}
}
}
}
答案 1 :(得分:0)
试试这个:
enum Mobile {
Samsung(400), Nokia(250),Motorola(325);
int price;
Mobile(int p) {
price = p;
}
int showPrice() {
return price;
}
}
public class EnumDemo {
public static void main(String args[]) {
System.out.println("CellPhone List:");
for(Mobile m : Mobile.values()) {
System.out.println(m + " costs " + m.showPrice() + " dollars");
}
Mobile ret = Mobile.Samsung;
System.out.println("The ordinal is = " + ret.ordinal());
System.out.println("MobileName = " + ret.name());
}
}
输出将是:
CellPhone List:
Samsung costs 400 dollars
Nokia costs 250 dollars
Motorola costs 325 dollars
The ordinal is = 0
MobileName = Samsung