File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert from enum ordinal/constant to enum type

 
will zhang
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to get enum type based on the code(an int)
Here's what I have using switch:



Is there any way I can use instead of switch?

I saw something here: http://stackoverflow.com/questions/609860/convert-from-enum-ordinal-to-enum-type
But I couldn't understand the second method, any hint?

Thanks
 
John de Michele
Rancher
Posts: 600
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will:

You could create a static Map with your codes as keys and your enums as values.

John.
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or loop over the array returned by values():
It's a bit of a choice which one to use, the loop or the Map. The Map is faster (O(1)) but requires extra memory to store it. The loop doesn't need the memory but needs to loop each time (O(n)).
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic