GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Iteration on Enum Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Iteration on Enum" Watch "Iteration on Enum" New topic
Author

Iteration on Enum

Prathamesh Gaddam
Ranch Hand

Joined: Feb 18, 2008
Posts: 58
Hi,

1. Please confirm how we can iterate on Enum values()through general for/while loops.

2. I'm able to use values() method on Enum though as per Javadoc Enum class do not have such method.

3. Which all operators/operations can be performed on instance of Enum.

Personal Experience: Its difficult to search the Javaranch for any existing posts as (may be) search is words based instead of search on the meaning of the group of words. Take a example of the above queries. For first query I searched was approx. 25-35 min but unable find required details.

Thank you in advance.

"Willer's will help the willer to Win !"
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Prathamesh,

A good overview to Enums can be found here.

I believe that values() is added by the compiler, and that's why you don't see it declared in java.lang.Enum. I'm not sure why it's not declare in the API.

To iterate over values of an enum, just use values(); example assumes proper declaration of public Enum Month {...}:



There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

This is specified in section 8.9 of the Java Language Specification.
...if E is the name of an enum type, then that type has the following implicitly declared static methods:
...
public static E[] values();
...
public static E valueOf(String name);


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39095
    
  23
Originally posted by Stevi Deter:
Prathamesh,

A good overview to Enums can be found here.
That's the tutorial with "glorified integers" in. I like it!

Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Thanks for the link, Marc. I knew it had to be documented somewhere. I always forget to check the language specification.
Prathamesh Gaddam
Ranch Hand

Joined: Feb 18, 2008
Posts: 58
Thank you Marc and Stevi.

Stevi,

I need the iterate on Enum using traditional for loop "for(; " or while loop. I'm able to use with advanced for loop. However unable to use

enum Color{RED, BLUE, GREEN};

for(Color c = Color.RED; c.ordinal() <= Color.GREEN; ? ){}

Please assist towards condition check and increment/decrement operations in for loop.
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Prathamesh,

Look into using EnumSet or EnumMap

Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Prathamesh Gaddam]: I need the iterate on Enum using traditional for loop "for(; " or while loop.

Well, Color.values() returns a simple Color[] array. You can iterate through this the same way you would iterate through any array. Normally in JDK 5 and later that's with an enhanced for loop, but it's also possible with a traditional for loop:


"I'm not back." - Bill Harding, Twister
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Iteration on Enum