enum: inverse of ordinal() method
807569May 18 2006 — edited May 18 2006In the enums exists the ordinal() method. My question is if exist the inverse of ordinal(), where from the integer value (the one returned from ordinal()) I get as the return value the enum value. I implemented it as in the code below, but I have to define it (exactly the same way) in every enum.
public enum Day {
MON,
TUE,
WED,
THR,
FRI,
SAT,
SUN;
public static Day Type(int ordinal) {
Day[] vals = values();
return (ordinal>=0 && ordinal<vals.length) ? vals[ordinal] : vals[0];
}
for example
Day d = Day.Type(3);
assign the value THR to the d variable as in
Day d = Day.THR;
Thanks in advance for any answer