有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将整数数组转换为枚举数组?

我有一个整数数组,需要将其转换为枚举数组

enum my_enum {
    APPLE, BANANA, ORANGE;
    int getColorAsInt() { ... }
};
int[] foo = some_method_i_cannot_modify();
my_enum[] bar = ??? ( foo );

最简单的方法是什么

Here我找到了一种将单个整数转换为枚举值的方法(他们在示例中使用了Color枚举):

public static Color convertIntToColor(int iColor) {
    for (Color color : Color.values()) {
        if (color.getColorAsInt() == iColor) {
            return color;
        }
    }
    return null;
}

。。。但是我希望有一种更直接的方法来进行转换(否则在我的代码中,首先使用枚举是没有意义的)

Here是一个关于将单个整数转换为枚举值的问题


共 (6) 个答案

  1. # 1 楼答案

    如果它是你的enum,你当然可以先给它映射

    public enum MyEnum {
        APPLES(10), ORANGES(20);
    
        private int asInt;
        private MyEnum(int val) {
            asInt = val;
        }
        public int getIntValue() { return asInt; }
    
        static final Map<Integer, MyEnum> mapping = new HashMap();
        static {
            for (MyEnum e : MyEnum.values()) {
                mapping.put(e.getIntValue(), e);
            }
        }
        static MyEnum fromInteger(int val) {
            return mapping.get(val);
        }
    }
    
  2. # 2 楼答案

    but I hope there is a more direct way to do that conversion

    你别无选择。因为您有int个值,并且希望将它们转换为枚举值
    因此,对于每一个int,你必须找到对应的枚举值,不管怎样

    在声明中使用enum的顺序有一种更直接的方法
    但这是一种非常容易出错的方法,它假设int值从0开始,然后递增1

  3. # 3 楼答案

    这取决于foo数组中的内容。如果它是枚举的ordinal,那么像这样简单的东西就足够了

    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));
    }
    

    印刷品

    [APPLE, ORANGE, ORANGE, BANANA]

    一个streams等价物应该是这样的:

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

    我会制作一张地图,从那里获取一个映射值。当然,使用自己的id比ordinal好,但为了表达一个想法:

    enum Color {
        RED, GREEN, BLUE;
    
        public static Color[] create(int[] ids) {
            Map<Integer, Color> colors = Arrays.stream(Color.values())
                    .collect(Collectors.toMap(Enum::ordinal, Function.identity()));
            return Arrays.stream(ids)
                    .mapToObj(colors::get)
                    .filter(Objects::nonNull)
                    .toArray(Color[]::new);
        }
    };
    
    public static void main(String[] args) {
        int[] foo = new int[]{0,1,2};
        System.out.println(Arrays.toString(Color.create(foo)));
    }
    

    输出

    [RED, GREEN, BLUE]
    
  5. # 5 楼答案

    您需要使用循环或流来迭代整数的输入数组,并将每个整数映射到Color实例。试试这个:

    int[] array = getArrayOfInts();
    Color[] colors = Arrays.stream(array)
                .mapToObj(i -> convertIntToColor(i))
                .filter(Objects::nonNull)
                .toArray(Color[]::new);
    
  6. # 6 楼答案

    这一行代码将返回对象数组,您可以访问enum 按arr[0]

    Object[] arr = Arrays.stream(my_enum.values()).map(x->x).toArray();