如何在java中获取枚举的所有可能值?(不知道具体的枚举)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6586175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get all possible values of an enum in java? (not knowing the specific Enum)
提问by Se Norm
I'd like to create a JComboBox that handles the selection of any Enum given to it. For that I need a method to retrieve all the available values of the Enum passed to the JComboBox. As I don't know the specific Enum I can't call EnumType.values()
.
I could think of some complicated solutions where supported Enums would have to implement some interface I define, but I guess I am missing a simpler, more general solution. What is the way I should go?
我想创建一个 JComboBox 来处理给它的任何 Enum 的选择。为此,我需要一种方法来检索传递给 JComboBox 的 Enum 的所有可用值。因为我不知道具体的 Enum 我不能调用EnumType.values()
. 我可以想到一些复杂的解决方案,其中受支持的枚举必须实现我定义的一些接口,但我想我缺少一个更简单、更通用的解决方案。我应该走什么路?
采纳答案by irreputable
回答by AZ_
Just one line of code
只需一行代码
List<SOME_ENUM> enumList = Arrays.asList(SOME_ENUM.values());