Use the built-in enumeration methods.
enum Apple {
A, B, C, D, E
}
public class EnumDemo2 {
public static void main(String args[])
{
Apple ap;
System.out.println("Here are all Apple constants");
// use values()
Apple allapples[] = Apple.values();
for(Apple a : allapples)
System.out.println(a);
System.out.println();
// use valueOf()
ap = Apple.valueOf("A");
System.out.println("ap contains " + ap);
}
}
Related examples in the same category