Cava.B
Cava.B

Reputation: 15

Maximum value in array containing Objects Java

I have an array named trick of type Card which stores the Suit and the Rank. Is there a way to use Collections.max to find the highest ranked card? I have used ordinals in other aspects of my code but cant seem to get it to work now.

I have tried this, (t is the parameter passed into the method. trick is the array containing the Cards)

Collections.max(t.trick.rank.ordinal());

Its part of an else if statement which then goes onto another so I don't think I can start it with a for loop and as its part of a inherited class I cant do the for loop else where as it throws errors due to it not being inherited from the other class. Is there a simple way of finding my highest ranked card?

Upvotes: 0

Views: 1982

Answers (3)

Rainbacon
Rainbacon

Reputation: 943

One of the overloads for Collections.max() takes a comparator as a second parameter. Using this you can implement your own comparator class like this:

class ValueComparator implements Comparator<Card> {
    @Override
    public int compare(Card a, Card b)
    {
        return a.rank > b.rank ? 1 : a.rank == b.rank ? 0 : -1;
    }
}

Then you can call max with:

Card maximum = Collections.max(trick, new ValueComparator());

The value of doing it this way is that you can create multiple comparators and use them to do comparison-based operations(max, sort, etc...) for a variety of comparison types. For example if you wanted to compare on the suit of the card you could implement a suit comparator. This Question and its answers provide some very helpful examples of how to use comparators.

Upvotes: 2

omairysdev
omairysdev

Reputation: 11

It depends type of data the Java arrays are indexed by int. The maximum positive int in Java is 2^31 – 1 = 2,147,483,647. And the platform-specific limits can be really close to this number – for example 64bit MB Pro on Java 1.7 I can happily initialize arrays with up to 2,147,483,645 or Integer. MAX_VALUE-2 elements.

I think this is not a good practice. But it is a good information!

Upvotes: 1

ForeverStudent
ForeverStudent

Reputation: 2537

I would recommend you have your Card class implement the Comparable interface. This requires you to define a comparison function between Card instances.

Then all you will need to do to get the "biggest" card is call Collections.max on it.

a complete but minimalistic solution would look like this:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestingClass 
{

public static void main(String[] args) throws Exception 
{
    List <Card> a = new ArrayList<Card>();
    a.add(new Card(1));
    a.add(new Card(5));
    a.add(new Card(2));
    a.add(new Card(3));

    Card x= Collections.max(a);
    System.out.println(x.rank); //this will print 5
}

}

class Card implements Comparable<Card>
{
    public int rank;
    public Card(int rank)
    {
        this.rank=rank;
    }
     @Override
        public int compareTo(Card other) {
            if(this.rank<other.rank) return -1;
            else if(other.rank< this.rank) return 1;
            return 0;
        }

}

Upvotes: 2

Related Questions