1

How do you use an enum for an array subscript?

enum {VALUE_ONE, ... , SIZE};
int[] x = new int[SIZE];
2
  • please explain a bit more about your question. Each enum has a static method values() to get all its values.and use ordinal() to get it's position in enum. Commented Oct 14, 2014 at 8:13
  • I want to use a set of constant values with names like in an enum to iterate through an array. Commented Oct 14, 2014 at 8:14

5 Answers 5

2

Here's the code. It doesn't look like good practice, though.

int[] x = new int[EnumClass.values().length];

You can then access the element by ordinal()

int val = x[enumVar.ordinal()];

Still doesn't look like good practice.

Consider using Map<EnumClass, AtomicInteger> like EnumMap<EnumClass, AtomicInteger>. Why atomic integer? because it can have it's value modified instead of assigning a new instance all the time (at the cost of possibly unnecessary synchronization). That's a different issue though.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't know what you wan't to achieve. Map<Enum, AtomicInteger>? You have EnumMap designed for enums. I would not depend on enum's ordinal value for any data structure, since it depends entirely on the definition order. Suppose you save the array, modify the enum, and try to read the array. It'll be a problem.
1

Use an EnumMap - it was intended for just this case.

enum Value {
    VALUE_ONE, ... , VALUE_LAKH
}
Map<Value, Integer> x = new EnumMap<>();
x.put(Value.VALUE_ONE, 13);

Internally uses an array (Integer[]). The only disadvantage is using Integer instead of int. And assumedly java 9 or 10 will introduce primitive generic types (List<int> and such).

BTW EnumSet exists too, and is as efficient as BitSet.

Comments

0

Enum is not number. But you can do SIZE.ordinal()

Comments

0

For below enum

 enum test{VALUE_ONE, ... , SIZE};

Use SIZE like this

int[] x = new int[test.SIZE.ordinal()];

Comments

0

You can use: int[] x = new int[SIZE.ordinal()]; int var = x[SOME_VALUE.ordinal()]; Its position in its enum declaration, where the initial constant is assigned an ordinal of zero.

Or set special number for each enum elements. like:

enum A {
   VALUE_ONE(0),
   SIZE(1),
   ;

   int value;

   private A( int value) {
      this.value = value;
   }

   public int getValue() {
       return this.value;
   }
}

int[] x = new int[A.SIZE.getValue()];
int var = x[A.VALUE_ONE.getValue()];

5 Comments

When i try System.out.println( x[A.VALUE_ONE.getValue()] ); I get arrayOutOfBoundIndexException
VALUE_ONE(0), VALUE_TWO(1), I used these but they both resulted in 0 when printed when SIZE(2)
int[] x = new int[A.SIZE.getValue()]; int var1 = x[A.VALUE_ONE.getValue()];// var1 == 0 int var2 = x[A.VALUE_TWO.getValue()];// var2 == 0 You mean that?
I set var = x[A.VALUE_ONE.getValue()] and then printed var. I set var equal to the same thing but VALUE_TWO this time and they both printed out 0
did you fill array x? by default arrays fill with zero. your array is represented as [0, 0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.