*
The moose likes Beginning Java and the fly likes looping through an enum? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "looping through an enum?" Watch "looping through an enum?" New topic
Author

looping through an enum?

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 660

Rather than hand-typing each of the following enums, is there some way to loop through each value and read its .ordinal() value?




Jon
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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

// Yes the hint is intentionally.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2993
    
    9
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

Joined: Apr 25, 2008
Posts: 660

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

Joined: Apr 25, 2008
Posts: 660

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
Ranch Hand

Joined: Mar 05, 2008
Posts: 2993
    
    9
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
Ranch Hand

Joined: Mar 05, 2008
Posts: 2993
    
    9
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

Joined: Apr 25, 2008
Posts: 660

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
Ranch Hand

Joined: Mar 05, 2008
Posts: 2993
    
    9
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
Ranch Hand

Joined: Mar 05, 2008
Posts: 2993
    
    9
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

Joined: Apr 25, 2008
Posts: 660

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..

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3596
    
  14

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
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Or read this page.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: looping through an enum?
 
Similar Threads
[Error] Can only iterate over an array or an instance of java.lang.Iterable
java.lang.NullPointerException
Overriding equals() method within a subclass
static insights?
cannot read a Collection of a custom class as a parameter