Answer the question
In order to leave comments, you need to log in
How to get all values from an enum using a loop?
I have a list with fruits and their parameters. I need to use certain parameters with each type of fruit. In the "outTest" method, I call each element of the list 3 times and get "getName". Actually how to make it a loop and get "getName" from each element of the list?
public class Test{
public void outTest() {
System.out.print(fruit.pineapple.getName());
System.out.print(fruit.apple.getName());
System.out.print(fruit.lemon.getName());
}
public enum fruit {
pineapple ("Pineapple box", 1.2f, 4.5f, 8, 2),
apple ("Apple box", 0.4f, 3.2f, 6, 1),
lemon ("Lemon box", 0.2f, 5.2f, 3, 1);
private String name;
private float value;
private float rarity;
private int stage;
private int slot;
fruit(String name, float value, float rarity, int stage, int slot) {
this.name = name;
this.value = value;
this.rarity = rarity;
this.stage = stage;
this.slot = slot;
}
public String getName() {return name;}
public float getValue() {return value;}
public float getRarity() {return rarity;}
public int getStage() {return stage;}
public int getSlot() {return slot;}
}
}
Answer the question
In order to leave comments, you need to log in
for (fruit f : fruit.values()) {
System.out.println(f.getName());
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question