Is there a a C-like way to get item number from enum in java?
Asked Answered
M

5

8

Perhap this is a simple basic question

Having an enum

public enum TK{
        ID,GROUP,DATA,FAIL;
        }

Can I get the order number for example ID=0, GROUP=2, DATA=3, FAIL=4 ?

This is a way to to that, but a weird and long one! =S

public enum TK{
        ID(0),GROUP(1),DATA(2),FAIL(3);

        int num;
        TK(int n)
        {
           this.num=n;
        }

        public int get()
        {
           return num;
        }

  };

to get numbers so I write TK.ID.get(), TK.GROUP.get(), etc... I don't like that

there is a better way?

( C enums, C macros..I miss you both )

thanks

Multiflorous answered 4/5, 2010 at 13:29 Comment(4)
So much answers, yet none bothers to explain you shouldn't use ordinal in most cases. You don't want your program to depend on the ordering of your enum values.Charter
@Jorn: the API Docs say "... 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."Lorielorien
Sometimes it's not that bad, in my case I need to enum Titles of a csv table so if this would change is because the table did, and that's not very probable, at most new columns will be added at the end, and ordinal will still be useful I thinkGaiety
@Lorielorien exactly, that's why I mention it ;)Charter
H
7

Are you looking for ordinal?

Humane answered 4/5, 2010 at 13:32 Comment(1)
Yes, I was looking for that! (it don't let a clean code as C...) but well, it's java haha, thanksGaiety
U
14

The ordinal() does what you want to do. This is an excerpt from the documentation:

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.

Josh Bloch in Effective Java 2nd Edition goes further to explain why using ordinal() is a terrible idea. Here are some quotes, slightly edited:

Never derive a value associated with an enum from its ordinal; store it in an instance field instead. (Item 31: Use instance fields instead of ordinals) It is rarely appropriate to use ordinals to index arrays: use EnumMap instead. The general principle is that application programmers should rarely, if ever, use Enum.ordinal. (Item 33: Use EnumMap instead of ordinal indexing)

Your "weird and long" way is precisely the prescription of Item 31.

Fortunately, Java is not C. A Java enum is very powerful and flexible, supported by many library classes, and you should learn to embrace them instead of using ordinal().

Have a look at EnumMap for example.

A specialized Map implementation for use with enum type keys. All of the keys in an enum map must come from a single enum type that is specified, explicitly or implicitly, when the map is created. Enum Maps are represented internally as arrays. This representation is extremely compact and efficient.

That is, instead of the following (which is perhaps what you wanted to do):

T[] arr = ...;
TK tk = ...;
T something = ...;

arr[tk.ordinal()] = something;

You can instead do:

Map<TK,T> map = new EnumMap<TK,T>(TK.class);
TK tk = ...;
T something = ...;

map.put(tk, something);

The book also covers another "traditional" (ab)use of enum in C, i.e. bit fields (assigning powers of 2 to each constant etc). Well, for that Java has EnumSet instead.

Uwton answered 4/5, 2010 at 14:19 Comment(2)
@polygenelubricants: +1 but just like "most programmers will have no use for Integer's numberOfLeadingZeros()". I definitely have a lot of very valid use for ordinal() and I'm very glad not to be "most programmers" :)Colcannon
+1 thanks for long answer, I am just reading "Effective Java" ! but I haven't still reached Item 33!! =)Gaiety
H
7

Are you looking for ordinal?

Humane answered 4/5, 2010 at 13:32 Comment(1)
Yes, I was looking for that! (it don't let a clean code as C...) but well, it's java haha, thanksGaiety
L
2

You can use ordinal()

Legerdemain answered 4/5, 2010 at 13:33 Comment(0)
L
2
tk.ordinal()

is the way to do it where tk is an instance of TK

http://java.sun.com/javase/6/docs/api/index.html?java/lang/Enum.html#ordinal()

Lorielorien answered 4/5, 2010 at 13:33 Comment(0)
B
1

http://java.sun.com/javase/6/docs/api/java/lang/Enum.html#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.

Broca answered 4/5, 2010 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.