
Loop Through An Enum in Java
Loop Through an Enum in Java
In Java, enums (short for "enumerations") are a special data type that represents a collection of constants. You can loop through the constants of an enum using several methods, like using the values()
method, which returns all the enum constants in the order they are declared.
Here's how you can loop through an enum in Java:
Example: Enum and Looping Through Enum Constants
1. Define an Enum
First, let's define a simple enum called Day
that represents the days of the week.
public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}
2. Loop Through the Enum Using values()
Method
The values()
method is automatically generated by the Java compiler for all enums. It returns an array of the enum constants, which you can loop through using a for
loop.
public class EnumLoopExample { public static void main(String[] args) { // Looping through the enum values using the values() method for (Day day : Day.values()) { System.out.println(day); } }}
Explanation:
The
Day.values()
method returns an array of all theDay
enum constants.The
for
loop iterates through each constant (SUNDAY
,MONDAY
, etc.) and prints it.
Output:
SUNDAYMONDAYTUESDAYWEDNESDAYTHURSDAYFRIDAYSATURDAY
3. Loop Through Enum and Perform Actions
You can also perform specific actions during the loop. For example, checking the enum value and performing different operations:
public class EnumLoopExample { public static void main(String[] args) { for (Day day : Day.values()) { // Perform an action based on the day switch (day) { case MONDAY: System.out.println("Start of the week!"); break; case FRIDAY: System.out.println("Almost weekend!"); break; case SUNDAY: System.out.println("Relax, it's Sunday."); break; default: System.out.println(day + " is a working day."); } } }}
Output:
Start of the week!MONDAY is a working day.TUESDAY is a working day.WEDNESDAY is a working day.THURSDAY is a working day.Almost weekend!SUNDAY is a working day.Relax, it's Sunday.
Additional Information
values()
Method: This is an implicit method that gets automatically generated for all enums and returns an array of all the constants.Enum Ordinal Values: Every enum constant has an implicit ordinal value (its position in the enum declaration). You can access it using the
ordinal()
method.
Example using ordinal()
:
public class EnumLoopOrdinal { public static void main(String[] args) { for (Day day : Day.values()) { System.out.println(day + " is at position " + day.ordinal()); } }}
Output:
SUNDAY is at position 0MONDAY is at position 1TUESDAY is at position 2WEDNESDAY is at position 3THURSDAY is at position 4FRIDAY is at position 5SATURDAY is at position 6
Conclusion
You can easily loop through enums in Java using the values()
method, and you can perform any operation on each constant during the iteration. Enums are a powerful feature in Java, especially when you need to define a fixed set of constants, such as days of the week, months of the year, etc.
Let me know if you need any further details or examples!