Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / June 2005

Tip: Looking for answers? Try searching our database.

Enums

Thread view: 
Andreas Nicoletti - 23 Jun 2005 19:23 GMT
I have the following questions about Java 1.5 enums.

1. the ordinal() method allows the conversion of enum value to int. Is
there a method/technique to go from int to enum? I have tried casting
but that does not seem to work.

2. is there an iterator for enums?

Thank you for your attention.
Stefan Schulz - 23 Jun 2005 19:39 GMT
> I have the following questions about Java 1.5 enums.
>
> 1. the ordinal() method allows the conversion of enum value to int. Is
> there a method/technique to go from int to enum? I have tried casting
> but that does not seem to work.

Well, it is not exactly killing performance, but values()[num] should do
the trick.

> 2. is there an iterator for enums?

again, iterate over values(). This is an array, and therefore iterable.

Signature

You can't run away forever,
But there's nothing wrong with getting a good head start.
          --- Jim Steinman, "Rock and Roll Dreams Come Through"
         

Paul - 24 Jun 2005 20:46 GMT
>I have the following questions about Java 1.5 enums.
>
[quoted text clipped - 5 lines]
>
> Thank you for your attention.

There is a static method values() on enum types that returns an array of the
enum's values in declared order.
You can iterate over an array using the enhanced for loop, but it's not an
actual Iterator instance.

As for question 1:
You could also create a method of your enumerated type to return the
instance of the int you want. If you use a constructor then you will ruin
the singleton pattern generated by the enum type declaration.

public enum OneOfThree
{
 ONE, TWO, THREE;

 public OneOfThree get(int i) throws Exception
 {
   switch (i)
   {
     case ONE.ordinal(): return ONE;
     case TWO.ordinal(): return TWO;
     case THREE.ordinal(): return THREE;
     default: throw new Exception("Invalid argument");
   }
 }
}

I'm sure others have different ways to do this, too.
You could also return the instance from the values() array at the ordinal
index passed,
 return values()[i]; // if 'i' is in range

Also see:
http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

--Paul


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.