如何获取 public 内部枚举的所有元素的列表?

How can I get a list of all elements of a public inner enum?

鉴于此 class:

public class Outer {

  public enum Day {
      SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
  }

}

我尝试了 new Outer().Day.values();new Outer().Day.getEnumConstants(); 但我得到 Day cannot be resolved or is not a field

知道如何访问它吗?

您可以按如下方式访问:Outer.Day.values(); 枚举 Day 可以像静态变量一样公开访问。

我 运行 您的代码使用 IDEONE,这是我的解决方案(根据您的需要进行调整):

public enum Day {
      SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
  }
    public static void main (String[] args) throws java.lang.Exception
    {
        Day[] d = Outer.Day.values();
        for (Day dd : d){
            System.out.println(dd);
        }
    }

link 是 - http://ideone.com/B1qLKg

这是你想要的吗?