How to get an enum value from a string value in Java(如何从Java中的字符串值中获取枚举值)
问题描述
假设我有一个枚举,它只是
公共枚举 Blah {A B C D}我想找到一个字符串的枚举值,例如 A",它是 Blah.A.怎么可能做到这一点?
是 Enum.valueOf() 我需要的方法?如果是这样,我将如何使用它?
是的,Blah.valueOf("A") 会给你Blah.A.p>
请注意,名称必须是精确匹配,包括大小写:Blah.valueOf("a") 和 Blah.valueOf("A") 都抛出 IllegalArgumentException.
静态方法 valueOf() 和 values() 是在编译时创建的,不会出现在源代码中.不过,它们确实出现在 Javadoc 中;例如,Dialog.ModalityType代码> 显示这两种方法.
Say I have an enum which is just
public enum Blah {
    A, B, C, D
}
and I would like to find the enum value of a string, for example "A" which would be Blah.A. How would it be possible to do this?
Is the Enum.valueOf() the method I need? If so, how would I use this?
Yes, Blah.valueOf("A") will give you Blah.A.
Note that the name must be an exact match, including case: Blah.valueOf("a") and Blah.valueOf("A ") both throw an IllegalArgumentException.
The static methods valueOf() and values() are created at compile time and do not appear in source code. They do appear in Javadoc, though; for example, Dialog.ModalityType shows both methods.
这篇关于如何从Java中的字符串值中获取枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从Java中的字符串值中获取枚举值
 
				
         
 
            
        - GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 获取数字的最后一位 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 转换 ldap 日期 2022-01-01
 
						 
						 
						 
						 
						 
				 
				 
				 
				