1

Say I have enum values:

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

Using the enhanced for loop construct, how can I write a code fragment that prints all the days of the week. I'm new to enhanced for loops so i don't know where to start.

0

3 Answers 3

3

enhanced for loop construct

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.

 for(Day days: Day.values()){

      System.out.println(days); // printing days
 }
Sign up to request clarification or add additional context in comments.

Comments

1

This is how you can use for loop to iterate over all enum constants.

for (Day day : Day.values()) {

    //your code
    //Use variable "day" to access each enum constant in the loop.

}

Comments

1

What you can do is :

for (Day day : Day.values()) {
    System.out.println(day);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.