在Java中从枚举中获取字符串值

时间:2023-01-26 14:24:40

I have a enum defined like this and I would like to be able to obtain the strings for the individual statuses. How should I write such a method?

我有一个这样定义的枚举,我希望能够获得各个状态的字符串。我该怎么写这样的方法?

I can get the int values of the statuses but would like the option of getting the string values from the ints as well.

我可以获取状态的int值,但是也希望从int中获取字符串值。

public enum Status {
    PAUSE(0),
    START(1),
    STOP(2);

    private final int value;

    private Status(int value) {
        this.value = value
    }

    public int getValue() {
        return value;
    }
}

5 个解决方案

#1


84  

if status is of type Status enum, status.name() will give you its defined name.

如果status是Status enum类型,status.name()将为您提供其定义的名称。

#2


46  

You can use values() method:

您可以使用values()方法:

For instance Status.values()[0] will return PAUSE in your case, if you print it, toString() will be called and "PAUSE" will be printed.

例如,Status.values()[0]将在您的情况下返回PAUSE,如果您打印它,将调用toString()并打印“PAUSE”。

#3


10  

Use default method name() as given bellows

使用默认方法名称()作为给定的波纹管

public enum Category {
        ONE("one"),
        TWO ("two"),
        THREE("three");

        private final String name;

        Category(String s) {
            name = s;
        }

    }

public class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Category.ONE.name());
    }
}

#4


5  

You can add this method to your Status enum:

您可以将此方法添加到Status枚举:

 public static String getStringValueFromInt(int i) {
     for (Status status : Status.values()) {
         if (status.getValue() == i) {
             return status.toString();
         }
     }
     // throw an IllegalArgumentException or return null
     throw new IllegalArgumentException("the given number doesn't match any Status.");
 }

public static void main(String[] args) {
    System.out.println(Status.getStringValueFromInt(1)); // OUTPUT: START
}

#5


0  

I believe enum have a .name() in its API, pretty simple to use like this example:

我相信枚举在其API中有一个.name(),这个例子使用起来非常简单:

private int security;
public String security(){ return Security.values()[security].name(); }
public void setSecurity(int security){ this.security = security; }

    private enum Security {
            low,
            high
    }

With this you can simply call

有了它,你可以简单地打电话

yourObject.security() 

and it returns high/low as String, in this example

并且它在此示例中以String返回高/低

#1


84  

if status is of type Status enum, status.name() will give you its defined name.

如果status是Status enum类型,status.name()将为您提供其定义的名称。

#2


46  

You can use values() method:

您可以使用values()方法:

For instance Status.values()[0] will return PAUSE in your case, if you print it, toString() will be called and "PAUSE" will be printed.

例如,Status.values()[0]将在您的情况下返回PAUSE,如果您打印它,将调用toString()并打印“PAUSE”。

#3


10  

Use default method name() as given bellows

使用默认方法名称()作为给定的波纹管

public enum Category {
        ONE("one"),
        TWO ("two"),
        THREE("three");

        private final String name;

        Category(String s) {
            name = s;
        }

    }

public class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Category.ONE.name());
    }
}

#4


5  

You can add this method to your Status enum:

您可以将此方法添加到Status枚举:

 public static String getStringValueFromInt(int i) {
     for (Status status : Status.values()) {
         if (status.getValue() == i) {
             return status.toString();
         }
     }
     // throw an IllegalArgumentException or return null
     throw new IllegalArgumentException("the given number doesn't match any Status.");
 }

public static void main(String[] args) {
    System.out.println(Status.getStringValueFromInt(1)); // OUTPUT: START
}

#5


0  

I believe enum have a .name() in its API, pretty simple to use like this example:

我相信枚举在其API中有一个.name(),这个例子使用起来非常简单:

private int security;
public String security(){ return Security.values()[security].name(); }
public void setSecurity(int security){ this.security = security; }

    private enum Security {
            low,
            high
    }

With this you can simply call

有了它,你可以简单地打电话

yourObject.security() 

and it returns high/low as String, in this example

并且它在此示例中以String返回高/低