Which of the following is the correct way to iterate over an enum in Java?

  1. Using a for-each loop on Enum.values()
  2. Using a while loop on Enum.length()
  3. Using Enum.iterator()
  4. Using Enum.getList()
Correct answer Using a for-each loop on Enum.values()

Explanation

You can use a for-each loop on `Enum.values()` to iterate over all enum constants.

Update: Dhruva Jagannath added a tag on this question

Topics

By Dhruva Jagannath · · 64 views