This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Get Enum By Ordinal Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Get Enum By Ordinal" Watch "Get Enum By Ordinal" New topic
Author

Get Enum By Ordinal

Jason Ferguson
Ranch Hand

Joined: Aug 09, 2007
Posts: 58
I have a Enum named condition.

The ordinal value for Condition.A is 0. The ordinal() method will return the ordinal value of 0.

However, if I have the value of 0, is there a way I can get Condition.A from it without a switch statement?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Yes. Condition.values() returns an array of all values in order by ordinal. So Condition.values()[0] returns the Condition for ordinal 0.


"I'm not back." - Bill Harding, Twister
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Get Enum By Ordinal
 
Similar Threads
Start java enum ordinal at 1 instead of 0
Assigning values to instance fields
Java "enum" : can you use non-consecutive ordinal numbers ?
Ordinal Value of ENUM and JPA
"casting" an int to an enum type