Q:

Java program to access enum constants using \'for each\' loop

belongs to collection: Java Enums Programs

0

Java program to access enum constants using 'for each' loop

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

// 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());
    }
  }
}

Output:

BIKE at index 0
CAR at index 1
BUS at index 2

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java program to create an enum class with construc... >>
<< Java program to create the main() method inside th...