Salmononius2 Salmononius2 -3 years ago 110
Java Question

Adding all the elements from an enum into an ArrayList

Is there a way to iterate through all the elements of an enum to add them into a LinkedList?

The example that I'm trying to do: I'm making a deck of cards. I have 2 enums: rank and value. Using a nested for loop, I want to have every card of the deck added into the ArrayList, using the 2 enums.


Answer Source

You can simply use the .values() static function of the enum class.

You could use:

for(Rank rank : Rank.values()) {
  for (Value val : Value.values()) {
    // stuff
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download