Java Enums: List enumerated values from a Class<? extends Enum>

前端 未结 4 1314
栀梦
栀梦 2020-12-09 14:14

I\'ve got the class object for an enum (I have a Class) and I need to get a list of the enumerated values represented by this enum. The

4条回答
  •  粉色の甜心
    2020-12-09 15:02

    If you know the name of the value you need:

         Class klass = ... 
         Enum x = Enum.valueOf(klass, "NAME");
    

    If you don't, you can get an array of them by (as Tom got to first):

         klass.getEnumConstants();
    

提交回复
热议问题