Enumerations, enhanced for loop
本问题已经有最佳答案,请猛点这里访问。
假设我有
1 2 3 4 | public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } |
使用增强的for循环结构,我如何编写一个代码片段来打印一周中的所有时间。我刚开始对循环进行增强,所以不知道从哪里开始。
增强的for循环构造
The enhanced for-loop is a popular feature introduced with the Java SE
platform in version 5.0. Its simple structure allows one to simplify
code by presenting for-loops that visit each element of an
array/collection without explicitly expressing how one goes from
element to element.
1 2 3 4 |
你能做的是:
1 2 3 |
这是使用for循环迭代所有枚举常量的方法。
1 2 3 4 5 6 | for (Day day : Day.values()) { //your code //Use variable"day" to access each enum constant in the loop. } |