Is there a way to loop through enums?

For an APEX class with:

public class EuchreCard{

    public enum suite {CLUBS, DIAMONDS, HEARTS, SPADES}
    public enum rank {NINE, TEN, JACK, QUEEN, KING, ACE}

}

Is there a way to loop through all enum values? For instance, I want to create a euchre hand, so all I need to do is loop through both suite and rank to generate every possible combination.

Answer

public enum suite {CLUBS, DIAMONDS, HEARTS, SPADES}
public enum rank {NINE, TEN, JACK, QUEEN, KING, ACE}

for(Integer i = 0; i < suite.values().size(); i++)
{
    for(Integer j = 0; j < rank.values().size(); j++)
    {
        system.debug(suite.values()[i] + ' ' + rank.values()[j]);   
    }
}

Attribution
Source : Link , Question Author : Scott Pelak , Answer Author : Boris Bachovski

Leave a Comment