java - Java 中枚举值表示的 Switch 语句

标签 java enums switch-statement

我非常熟悉在其他语言中使用枚举,但我在 Java 中遇到了一些特殊用途的困难。

Sun 的枚举文档大胆地指出:

"Java programming language enums are far more powerful than their counterparts in other languages, which are little more than glorified integers."

好吧,这很不错,但出于 switch 语句中比较的原因,我有点需要每个枚举都有一个常量数据类型表示。情况如下:我正在构建代表给定空间的节点,或迷宫图中的“插槽”,并且这些节点必须能够从代表迷宫的二维整数数组中构建。这是我为 MazeNode 类得到的,这是目前的问题所在(switch 语句咆哮):

注意:由于 case 语句中的动态项,我知道这段代码不起作用。它是为了说明我所追求的。

public class MazeNode
{
    public enum SlotValue
    {
        empty(0),
        start(1),
        wall(2),
        visited(3),
        end(9);

        private int m_representation;

        SlotValue(int representation)
        {
            m_representation = representation;
        }

        public int getRepresentation()
        {
            return m_representation;
        }
    }

    private SlotValue m_mazeNodeSlotValue;

    public MazeNode(SlotValue s)
    {
        m_mazeNodeSlotValue = s;
    }

    public MazeNode(int s)
    {

        switch(s)
        {
            case SlotValue.empty.getRepresentation():
                m_mazeNodeSlotValue = SlotValue.start;
                break;
            case SlotValue.end.getRepresentation():
                m_mazeNodeSlotValue = SlotValue.end;
                break;

        }
    }

    public SlotValue getSlotValue()
    {
        return m_mazeNodeSlotValue;
    }

}

所以代码在 switch 语句上提示“case 表达式必须是常量表达式”——我明白为什么编译器可能有问题,因为从技术上讲它们是动态的,但我不确定采用什么方法来解决这个。有没有更好的办法?

最重要的是,我需要枚举具有相应的整数值,以便与程序中传入的二维整数数组进行比较。

最佳答案

你可以这样使用:

import java.util.HashMap;
import java.util.Map;

public class MazeNode {

    public enum SlotValue {
        empty(0), start(1), wall(2), visited(3), end(9);

        protected int m_representation;

        SlotValue(int representation) {
            m_representation = representation;

        }

        private static final Map<Integer, SlotValue> mapping = new HashMap<Integer, SlotValue>();

        static {
            for (SlotValue slotValue : SlotValue.values()) {
                mapping.put(slotValue.m_representation, slotValue);
            }
        }

        public static SlotValue fromRepresentation(int representation) {
            SlotValue slotValue = SlotValue.mapping.get(representation);
            if (slotValue == null)
                // throw your own exception
                throw new RuntimeException("Invalid representation:" + representation);
            return slotValue;
        }
    }

    private SlotValue m_mazeNodeSlotValue;

    public MazeNode(SlotValue s) {
        m_mazeNodeSlotValue = s;
    }

    public MazeNode(int s) {
        m_mazeNodeSlotValue = SlotValue.fromRepresentation(s);

    }

    public SlotValue getSlotValue() {
        return m_mazeNodeSlotValue;
    }

    public static void main(String[] args) {
        MazeNode m = new MazeNode(2);
        System.out.println(m.getSlotValue());
        m = new MazeNode(9);
        System.out.println(m.getSlotValue());
    }

}

关于java - Java 中枚举值表示的 Switch 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1052244/

相关文章:

c# - Enum.HasFlag 中的意外结果

c++ - 如何在 COM 服务器中使用用户定义类型 (UDT)?

Javascript 开关始终默认

networking - Arista EOS 中的接口(interface)配置

java - 将值从 Java 传递给 javascript

java - Google Drive Java API V2 与 V3

c++ - 枚举运算符不工作

if-statement - 将 if 语句转换为 switch 语句

java - 从不同的类获取声明的变量

java - 如何将附加数据传递给 Mapper?