I have an array of integers and need to convert it to an array of enums.
enum my_enum {
APPLE, BANANA, ORANGE;
int getColorAsInt() { ... }
};
int[] foo = some_method_i_cannot_modify();
my_enum[] bar = ??? ( foo );
What is the easiest way to do that?
Here I found a way to convert a single integer to an enum value (they are using a Color
enum in their example):
public static Color convertIntToColor(int iColor) {
for (Color color : Color.values()) {
if (color.getColorAsInt() == iColor) {
return color;
}
}
return null;
}
... but I hope there is a more direct way to do that conversion (otherwise in my code it doesnt make sense to use an enum in the first place).
Here is a SO question about converting a single integer to enum value.