How do I loop through an enum in Java? -
this question has answer here:
- a 'for' loop iterate on enum in java 9 answers
let's have enum so:
public enum numbers { one("uno "), two("dos "), three("tres "); } private final string spanishname; numbers(string spanishname) { this.spanishname = spanishname; } public string getspanishname() { return spanishname; }
now have method outputs given variable.
public void output(string value) { printstream.print(value); }
i want use for
loop output values in enum. along lines of this:
for(/*each element in enum*/) { //output corresponding spanish name }
ultimate want final output uno dos tres
. how can using enums , loop?
for (numbers n : numbers.values()) { system.out.print(n.getspanishname() + " "); }
Comments
Post a Comment