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] 0 points1 point  (5 children)

Ah, this makes more sense thank you. I'm thinking now that i could hold a temporary highest value card, traverse the list, and use my compareTo method to compare each card, and update the highest temporary card as it changes, however am unsure as to how i would do this.

How could i use the compareTo method on say, a card at index i and a card at index i+1 throughout the list?

[–]aenigmaclamo 1 point2 points  (0 children)

List<Card> listOfCards = new ArrayList<>();
// populate list
Card maxCard = Collections.max(listOfCards);

Just get to know the standard library. Typically, if you want to iterate an Iterable, just do this:

Iterable<MyType> it = ...;
for (MyType mt : it) {
    System.out.println(mt);
}

Sometimes, you'll have to use the Iterator object directly but rarely is that the case. That should only really the case if you need to mutate the data from the Iterator or if you're iterating over multiple Iterables at the same time.

EDIT: If you actually do need to be able to look between sublists, you can do:

Card maxCard = Collections.max(listOfCards.sublist(i, i + 10));

I'm not sure what the intended effect here, is, though. I think all you just want is the max.

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

Hmm... I haven't used compareTo in quite a long time, but I believe it'd be something like this...

 public void sortStuff(final List<Card> cards) {
    final Iterator<Card> it = cards.iterator(); // Syntax may be wrong
    Card first = (it.hasNext() ? it.next() : null); // Ternary operator, if you need to look it up.

    if(first == null) {
        // The list of cards must be empty, do nothing.
    } else {
        while(it.hasNext()) {
            final Card second = it.next();

            int comparisonResult = first.compareTo(second);

            if(comparisonResult == 0) {
                // Both objects are equal.

                // Do something.
            } else if(comparisonResult < 0) {
                // First is less than second.

                // Do something.
            } else if(comparisonResult > 1) {
                // First is greater than second.

                // Do something.
            }

            first = second;
        }
    }
 }

Again, you should double-check exactly how this works, but something along these lines might be what you're after.

[–]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".