1

Can I do something like me[1] or MyEnum[1]? If I have:

enum MyEnum {e1,e2,e3,e4}; //... MyEnum me;

I have tried it, but it seems to generate errors. Is it another way to do it, or I can use just things like MyEnum.e2?

1
  • YourEnum.values() and iterate the array. Commented Dec 4, 2013 at 16:04

2 Answers 2

6

Can Java enum be iterated?

Yes, enum can be iterate with values() method

 for(MyEnum myenum: MyEnum.values()){
   // ..
 }

Can I do something like me[1] or MyEnum[1]?

MyEnum.values() return MyEnum[] where you can put index

MyEnum[] me=MyEnum.values();
MyEnum secVal= me[1]; //it will return e2.
Sign up to request clarification or add additional context in comments.

Comments

4

MyEnum.values() is what you might be looking for.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.