在Java中,可以使用values()
方法來遍歷enum中的所有值。以下是一個簡單的示例:
public class Main {
public enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
public static void main(String[] args) {
// 遍歷Day枚舉中的所有值
for (Day day : Day.values()) {
System.out.println(day);
}
}
}
上述代碼將輸出:
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
SUNDAY