Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

enum: inverse of ordinal() method

807569May 18 2006 — edited May 18 2006
In 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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 15 2006
Added on May 18 2006
2 comments
737 views