Back

Explore Courses Blog Tutorials Interview Questions
0 votes
7 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
1 answer
asked Nov 25, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
1 answer
asked Nov 23, 2019 in Java by Anvi (10.2k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.5k questions

32.5k answers

500 comments

108k users

Browse Categories

...