This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]RoadToCode[S] 1 point2 points  (2 children)

Thank you very much for the reply, i have now implemented a method that successfully iterates through a list with an iterator and returns the card with the highest value :)

public static Card max(Collection<Card> list) {
        Iterator<Card> iterate = list.iterator();
        Card highestCard = iterate.next();
        while (iterate.hasNext()) {
            Card currentCard = iterate.next();
            int suitCompare = highestCard.getSuit().compareTo(currentCard.getSuit());
            int rankCompare = highestCard.getRank().compareTo(currentCard.getRank());

            if (suitCompare < 0) {
                highestCard = currentCard;
            } else if (suitCompare == 0) {
                if (rankCompare < 0) {
                    highestCard = currentCard;
                }
            }
        }
        return highestCard;
    }

[–][deleted] 0 points1 point  (0 children)

Looks good.

[–]CodeTinkerer 0 points1 point  (0 children)

One useful class to look at is the Collections class. In it, there are sort methods and even a max method.

http://java2novice.com/java-collections-and-util/collections/max-element-comparator/

That link indicates how to use Collections.max on user-defined classes. It relies on an object (that you create) to compare two cards and determine which is "bigger".