Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework
this week in the
Java in General
forum!
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Liutauras Vilda
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Devaka Cooray
Paul Clapham
Saloon Keepers:
Scott Selikoff
Tim Holloway
Piet Souris
Mikalai Zaikin
Frits Walraven
Bartenders:
Stephan van Hulst
Carey Brown
Forum:
Java in General
Convert from enum ordinal/constant to enum type
will zhang
Ranch Hand
Posts: 46
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I want to get enum type based on the code(an int)
Here's what I have using switch:
public enum Type { AA(1050), BB(1051), CC(1052), DD(1053), EE(1054); private int code; Type(int c) { this.code = c; } public int getCode() { return code; } public static Type getType(int c) { switch (c) { case 1050: return AA; case 1051: return BB; case 1052: return CC; case 1053: return DD; case 1054: return EE; } return null; } }
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
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Will:
You could create a static Map with your codes as keys and your enums as values.
John.
Rob Spoor
Sheriff
Posts: 22816
132
I like...
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Or loop over the array returned by values():
public enum Type { AA(1050), BB(1051), CC(1052), DD(1053), EE(1054); private int code; Type(int c) { this.code = c; } public int getCode() { return code; } public static Type getType(int c) { for (Type type: values()) { if (type.code == c) { return type; } } return null; } }
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)).
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Enumeration.
Sorting question
static methods and NULL
generic question
Map Problem
More...