How to convert an array of integers to array of enums?

idclev 463035818

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.

OldCurmudgeon

It depends on what is in your foo array. If it is the ordinals of the enums then something as simple as this should suffice.

enum my_enum { APPLE, BANANA, ORANGE };
int[] foo = {0,2,2,1};
public void test(String[] args) {
    my_enum[] bar = new my_enum[foo.length];
    for (int i = 0; i < foo.length; i++) {
        bar[i] = my_enum.values()[foo[i]];
    }
    System.out.println(Arrays.toString(bar));
}

prints

[APPLE, ORANGE, ORANGE, BANANA]

A streams equivalent would be something like:

    my_enum[] es = Arrays.stream(foo)
            .mapToObj(i -> my_enum.values()[i])
            .toArray(my_enum[]::new);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert an array of integers to a tree?

How to convert an array of integers to an integer

How to convert an array of integers into an array of bits?

How to convert list of enums into array of strings?

How to convert an array of string integers to an array of integers? Ruby

How to convert a string representation to an array integers?

How do I convert an array of integers to binary?

How to convert an IP string to an array of integers in java?

How to convert an array of mixed sized integers in swift

How to convert an array of integers to a single digit

How can I convert the array of integers to rows?

How to Convert Range of Integers into PHP Array

How to convert multiple integers into byte array

how to convert array of integers into single float variable?

How to convert a tuple of an array to a tuple of integers

Convert array of integers to string

convert string into array of integers

Convert array of strings to an array of integers

Convert Array of objects to array of integers

Convert a typescript enum into an array of enums

Convert array of enums to array of its representative strings

How do I convert an array of integers to an array of objects?

How do I convert an array of floats into an array of unique integers?

how to convert array of integers after array_agg into values for IN clause

Convert array of integers into dictionary of indices

convert Date string to array of integers

Convert array of Strings to list of Integers?

Convert string to array of integers in golang

R: convert strings in array into integers