Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
9 views
in Java by (10.2k points)

I have an enum in Java for the cardinal & general directions:

public enum Direction {

   NORTH,

   NORTHEAST,

   EAST,

   SOUTHEAST,

   SOUTH,

   SOUTHWEST,

   WEST,

   NORTHWEST

}

How can I write a loop that iterates through each of these enum values?

1 Answer

0 votes
by (46k points)

Use values() on your enum:

Syntax:

for (Direction dir : Direction.values()) {

  // do your stuff

}

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
asked Nov 25, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
2 answers
asked Nov 23, 2019 in Java by Anvi (10.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...