将字符串转换为等效枚举值 [英] Convert String to equivalent Enum value
问题描述
我可以使用Java将 String
转换为枚举
中的等效值。 / p>
我当然可以使用一个大的 if-else
语句,但是如果可能,我想避免这种情况
鉴于此文档:
http://download.oracle.com/javase/1.4.2/docs/api/java/util/Enumeration.html
我不太希望这可能没有ifs或case语句。
希望你意识到, 您可以使用 因此,如果您的枚举被定义如下: 您可以这样做: Is it possible for me to convert a I can of course do this with a large Given this documentation: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Enumeration.html I am not too hopeful that this is possible without ifs or a case statement. Hope you realise, You can simply use Thus if your enum is defined as so: You could do this:
这篇关于将字符串转换为等效枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! java.util.Enumeration
不同于Java 1.5
YourEnum
公开枚举日{
星期日,星期一,星期四,星期四,
THURSDAY,FRIDAY,SATURDAY
}
String day =SUNDAY;
Day dayEnum = Day.valueOf(day);
String
to an equivalent value in an Enumeration
, using Java.if-else
statement, but I would like to avoid this if possible.java.util.Enumeration
is different from the Java 1.5 Enum types.YourEnum.valueOf("String")
to get the equivalent enum type.public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
String day = "SUNDAY";
Day dayEnum = Day.valueOf(day);