• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Iteration on Enum

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !"
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 {...}:

 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);

 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prathamesh,

Look into using EnumSet or EnumMap

 
Wanderer
Posts: 18671
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
reply
    Bookmark Topic Watch Topic
  • New Topic