Which statement will iterate through Direction?

Given:

public enum Direction {

NORTH, EAST, SOUTH, WEST

}

Which statement will iterate through Direction?

Given:

public enum Direction {

NORTH, EAST, SOUTH, WEST

}

Which statement will iterate through Direction?

A.
for (Direction d : Direction.values()){
//
}

B.
for (Direction d : Direction.asList()){
//
}

C.
for (Direction d : Direction.iterator()){
//
}

D.
for (Direction d : Direction.asArray()){
//
}

Explanation:
The static values() method of an enum type returns an array of the enum values. The foreach loop is a good way to go over all of them.
//… Loop over all values.
for (Direction d : Direction.values()){
System.out.println(d); // PrintsNORTH, EAST, …
}



Leave a Reply 2

Your email address will not be published. Required fields are marked *