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.
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.
the ordinal of this enumeration constant"
Java 1.6 API at http://download.oracle.com/javase/6/docs/api.
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.
I'm guessing the missing final word was "vague".
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...
public static <T extends Enum<T>> T valueOf(Class<T> enumType,
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.)
enumType - the Class object of the enum type from which to return a constant
name - the name of the constant to return
the enum constant of the specified enum type with the specified name
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
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..