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
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)

Browse Categories

...