This question already has an answer here: Lookup Java enum by string value 23 answers Try this out. I have created a method which searches type using id: public static Type getType(int id) { for (Type type : Type.values()) { if (id == type.getId()) { return type; } } return null; } 您可以构建一个映射来执行此查找。 static final Map<Integer, Type>
这个问题在这里已经有了答案: 通过字符串值查找Java枚举23个答案 尝试一下。 我创建了一个使用id搜索类型的方法: public static Type getType(int id) { for (Type type : Type.values()) { if (id == type.getId()) { return type; } } return null; } 您可以构建一个映射来执行此查找。 static final Map<Integer, Type> id2type = new HashMap<>(); static {
This question already has an answer here: Lookup Java enum by string value 23 answers 首先, Letter必须是一个枚举: enum Letter { A, B, C } Letter letter = Letter.valueOf("A") // and just switch switch (letter) { case A: case B: case C: } You can do it like this: String letter = "A"; switch (Letter.valueOf(letter)) { case A: // No problem! case B: case C: def
这个问题在这里已经有了答案: 通过字符串值查找Java枚举23个答案 首先, Letter必须是一个枚举: enum Letter { A, B, C } Letter letter = Letter.valueOf("A") // and just switch switch (letter) { case A: case B: case C: } 你可以这样做: String letter = "A"; switch (Letter.valueOf(letter)) { case A: // No problem! case B: case C: default: } 关于ideone的演示。 首先
This question already has an answer here: Lookup Java enum by string value 23 answers 您需要将String arg从命令行转换为枚举值。 Child c = Child.valueOf(args[0]); Use Enum.valueOf() . It takes an enum class and string as as argument and tries and find an enum by that name. Note: throws IllegalArgumentException if not found... You'll have to catch it explicitly since this is an unchecked
这个问题在这里已经有了答案: 通过字符串值查找Java枚举23个答案 您需要将String arg从命令行转换为枚举值。 Child c = Child.valueOf(args[0]); 使用Enum.valueOf() 。 它需要一个枚举类和字符串作为参数,并试图通过该名称找到一个枚举。 注意:如果未找到,则抛出IllegalArgumentException ...您必须明确地捕获它,因为这是一个未经检查的异常。 另一种解决方案是在你的枚举类本身上使用.valueOf() ( MyEnum.value
This question already has an answer here: Lookup Java enum by string value 23 answers Your question doesn't really make much sense. If you want to send or store list of enums then you can simply serialize and deserialize it since ArrayList and each enum are Serializable . Example: List<Day> ofiginalList = new ArrayList<Day>(); ofiginalList.add(Day.Monday); ofiginalList.add
这个问题在这里已经有了答案: 通过字符串值查找Java枚举23个答案 你的问题并没有多大意义。 如果你想发送或存储枚举列表,那么你可以简单地序列化和反序列化它,因为ArrayList和每个enum都是可Serializable 。 例: List<Day> ofiginalList = new ArrayList<Day>(); ofiginalList.add(Day.Monday); ofiginalList.add(Day.Tuesday); ByteArrayOutputStream bout = new ByteArrayOutputStream(); ObjectOutp
Possible Duplicate: Java - Convert String to enum I have a method that uses an enum: mymethod(AnotherClass.MyEnum.PassedEnum); And I want to nest this within a class that receives a String which becomes MyEnum: public static void method(String toPass){ mymethod(AnotherClass.toPass.PassedEnum); } The passed variable has to be a String but I need to convert it to a Enum to pass to Anothe
可能重复: Java - 将字符串转换为枚举 我有一个使用枚举的方法: mymethod(AnotherClass.MyEnum.PassedEnum); 我想在一个接收到一个字符串变成MyEnum的类中嵌套它: public static void method(String toPass){ mymethod(AnotherClass.toPass.PassedEnum); } 传递的变量必须是一个字符串,但我需要将其转换为Enum传递给AnotherClass? TIA 使用AnotherClass.MyEnum.valueOf(toPass) 你的意思是... MyEnum e = M
This question already has an answer here: Lookup Java enum by string value 23 answers You want a Map<String, Code> , but how to populate it neatly? Enums don't allow you to initialize a static fields before the enum instances are initialized, but there's a neat little trick, called the Initialization-on-demand holder idiom, that makes using a statically initialized map needed f
这个问题在这里已经有了答案: 通过字符串值查找Java枚举23个答案 你想要一个Map<String, Code> ,但是如何整齐地填充它? 枚举不允许你在枚举实例初始化之前初始化一个静态字段,但是有一个巧妙的小技巧,称为初始化按需持有者习惯用法,这使得使用这个功能所需的静态初始化映射易于实现: public enum Code { CODE_1("string1"), CODE_2("string2"), CODE_3("string3"), // etc ; private
It is not possible to make a static initialization before initialization of enum elements in Java since enum elements always have to be declared and initialized first. If a static initialization doesn't depend on enum elements, its execution order after elements is not a problem. Otherwise, if an initialization depends on enum elements, this would cause trouble. However, Java enums provid
在初始化Java枚举元素之前,不可能进行静态初始化,因为枚举元素总是必须首先声明和初始化。 如果静态初始化不依赖枚举元素,它在元素之后的执行顺序不成问题。 否则,如果初始化取决于枚举元素,这会造成麻烦。 但是,Java枚举提供了能够满足此依赖性的values()方法。 但是调用values()可能是一个开销,因为values()方法每次都复制内部静态数组。 例如(在后编辑为什么不能枚举的构造函数访问静态字段?): publi
I understand why an enum constructor cannot access static fields and methods within the enum itself, and why the same is allowed for in classes. Se for an example the following code, import java.util.ArrayList; import java.util.List; public enum Foo { A("Some string"), B("Some other string"), ; static List<String> list = new ArrayList<>(); Foo(String descripti
我明白为什么枚举构造函数不能访问枚举本身中的静态字段和方法,以及为什么类中允许使用静态字段和方法。 以下面的代码为例, import java.util.ArrayList; import java.util.List; public enum Foo { A("Some string"), B("Some other string"), ; static List<String> list = new ArrayList<>(); Foo(String description) { list.add(description); } } 此代码会导致编译时
I'm having trouble trying to fill a static map with values from arguments of an enum. Example of what I'm trying to say here: public enum LettersAndNumbers { A(1, 2), B(2, 3); private static HashMap<Integer, Integer> numbers = new HashMap<Integer, Integer(); private LettersAndNumbers(int numberone, int numbertwo) {} // Somehow put arguments "numbero
我无法使用枚举参数的值填充静态地图。 我想在这里说的例子: public enum LettersAndNumbers { A(1, 2), B(2, 3); private static HashMap<Integer, Integer> numbers = new HashMap<Integer, Integer(); private LettersAndNumbers(int numberone, int numbertwo) {} // Somehow put arguments "numberone" and "numbertwo" into map public static Integer getNumberTwo(int nu
This question already has an answer here: How to use fields in java enum by overriding the method? [duplicate] 8 answers Try this ((TestEnum)this).x The error message is a misleading. The problem is, private field is not inherited, therefore x or this.x won't compile. You have to up-cast it to the superclass to access it. see also https://groups.google.com/d/msg/java-lang-fans
这个问题在这里已经有了答案: 通过重写该方法如何使用Java枚举中的字段? [复制] 8个答案 尝试这个 ((TestEnum)this).x 错误消息是一种误导。 问题是, private字段没有被继承,因此x或this.x将不会被编译。 您必须将其上传到超类才能访问它。 另请参阅https://groups.google.com/d/msg/java-lang-fans/KVmmgeSCHkA/Kr_KxaaeolIJ 这里'受保护的'访问可能更好 protected int x;