The source code to access enum constants using "for each" loop is given below. The given program is compiled and executed successfully.
// Java program to access enum constants
// using "for each" loop
public class Main {
enum Vehicle {
BIKE,
CAR,
BUS
}
public static void main(String[] args) {
Vehicle arr[] = Vehicle.values();
for (Vehicle v: arr) {
System.out.println(v + " at index " + v.ordinal());
}
}
}
Program/Source Code:
The source code to access enum constants using "for each" loop is given below. The given program is compiled and executed successfully.
Output: