• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

looping through an enum?

 
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rather than hand-typing each of the following enums, is there some way to loop through each value and read its .ordinal() value?



 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you looked at the methods of an enum? Which of those could help to iterate through the values?

// Yes the hint is intentionally.
 
Master Rancher
Posts: 4062
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Errr, the method Jon needs isn't really listed under Enum, since it's a static method attached to each individual Enum subclass. Jon, you need to follow the link to JLS 8.9 where they talk about the valueOf() and values() methods.
 
Jon Camilleri
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Errr, the method Jon needs isn't really listed under Enum, since it's a static method attached to each individual Enum subclass. Jon, you need to follow the link to JLS 8.9 where they talk about the valueOf() and values() methods.



"ordinal

public final int ordinal()
Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero). Most programmers will have no use for this method. It is designed for use by sophisticated enum-based data structures, such as EnumSet and EnumMap.
Returns:
the ordinal of this enumeration constant"

Java 1.6 API at http://download.oracle.com/javase/6/docs/api.


 
Jon Camilleri
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wouter Oet wrote:Have you looked at the methods of an enum? Which of those could help to iterate through the values?

// Yes the hint is intentionally.



Excuse me?
 
Mike Simmons
Master Rancher
Posts: 4062
56
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Jon, your original post made it fairly clear you already know about the ordinal() method. But there is another method that you need, which will help address the problem you asked about, to avoid typing all those names. That's what I was pointing you towards.
 
Mike Simmons
Master Rancher
Posts: 4062
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jon Camilleri wrote:

Wouter Oet wrote:Have you looked at the methods of an enum? Which of those could help to iterate through the values?

// Yes the hint is intentionally.



Excuse me?


I'm guessing the missing final word was "vague".
 
Jon Camilleri
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Yes, Jon, your original post made it fairly clear you already know about the ordinal() method. But there is another method that you need, which will help address the problem you asked about, to avoid typing all those names. That's what I was pointing you towards.



The syntax looks more gory, I'm not sure how to translate it to shorter code...

"valueOf

public static <T extends Enum<T>> T valueOf(Class<T> enumType,
String name)
Returns the enum constant of the specified enum type with the specified name. The name must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
Parameters:
enumType - the Class object of the enum type from which to return a constant
name - the name of the constant to return
Returns:
the enum constant of the specified enum type with the specified name
Throws:
IllegalArgumentException - if the specified enum type has no constant with the specified name, or the specified class object does not represent an enum type
NullPointerException - if enumType or name is null
Since:
1.5"

http://download.oracle.com/javase/6/docs/api

 
Mike Simmons
Master Rancher
Posts: 4062
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, no, that wasn't it either. Keep reading. And is it really necessary to requote all this stuff here? I would think we're all able to follow links.
 
Mike Simmons
Master Rancher
Posts: 4062
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, if someone could edit the first post in this thread to remove all those the extra useless spaces at the end of line 29 of the code block, it would do wonders for making this thread readable again. It didn't look this bad when first posted, so I don't know what happened. But that's why we're all having to use the horizontal scroll bars now.
 
Jon Camilleri
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:By the way, if someone could edit the first post in this thread to remove all those the extra useless spaces at the end of line 29 of the code block, it would do wonders for making this thread readable again. It didn't look this bad when first posted, so I don't know what happened. But that's why we're all having to use the horizontal scroll bars now.



Sorry, if there's some spacing I can update let me know I didn't see anything out of place, I was just asking for an example..

 
Saloon Keeper
Posts: 13397
297
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jon, do you know how to use Javadoc?

Try making a Javadoc of your Suit or Rank classes. Then read the page, and see what methods are there.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or read this page.
 
reply
    Bookmark Topic Watch Topic
  • New Topic