• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Iteration on Enum

 
Prathamesh Gaddam
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 {...}:

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathamesh,

Look into using EnumSet or EnumMap

 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic