1//If performance is not an issue (code is only called a few times)
2//The reason this is expensive is that .values() returns an array,
3//which is a copy of the original because the array might be modified.
4MyEnum.values()[x]
5
6//If performance is an issue (code is run hundreds of times)
7public enum MyEnum {
8 EnumValue1,
9 EnumValue2;
10
11 public static MyEnum fromInteger(int x) {
12 switch(x) {
13 case 0:
14 return EnumValue1;
15 case 1:
16 return EnumValue2;
17 }
18 return null;
19 }
20}