我现在有个枚举的使用场景,请问各位大虾怎么处理1.有个枚举类型
public enum SystemType
{
windows(0);
linux(1);
}
某段程序能得到0,如何能取到对应的SystemType.windows枚举类

解决方案 »

  1.   

    你想问,比如根据0获取 window?
      

  2.   

        for (SystemType type : SystemType.values()) {
          if (type.ordinal() == 0) {
            System.out.println(type);
            break;
          }
        }
      

  3.   

    SystemType[] s = SystemType.values();
    for(SystemType s1:s)
    {
    if(s1.ordinal()== a)
    {
    SystemType target = s1;
    }
    }
    遍历下可以得到
      

  4.   

    public class TestAdd {
    public enum OS{
    linux(0),
    window(1);

    private final int value;
    private OS(int a){
    this.value = a;
    }
    }
    public static void main(String[] args) {
    for (OS prop : OS.values()){
    if(prop.value == 0){
    System.out.println(prop);
    }
    }
    }}
      

  5.   

    你可以在SystemType里面加一个方法:SystemType getSystemTypeFromInt(int i)
    {
        for (SystemType st : values())
        {
             if (st.ordinal() == i)
                 return st;
        }
       //无法转换
       return null;
    }这样勉强能达到你要的效果
      

  6.   

    1. for(SystemType myType: SystemType.values()){
    if(myType.ordinal() == 0){
    return myType;
    }
    }2.  if(type.ordinal() == 0){
    return SystemType.windows;
    }