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


Categories

...