TheRapture87
TheRapture87

Reputation: 1423

Find largest array element with a Comparator in Java

So I have an exam on Tuesday for Algorithms and Data but I can't solve this question from a past paper.

Write a Java static method called greatest which takes an array of objects and a Comparator object which can order objects of the array’s element type, and returns whichever element from that array is greatest according to the order given by the Comparator object. You may assume the array is of length at least 1. Your code must use no method from Java’s API except the method compare from type Comparator.Your method must be generic, so the array could have any non-primitive element type.

My Attempt:

public static void greatest(Object[] a, Comparator<Object> x) {
        for (int i = 0; i < a.length; i++) {
            x.compare(a[i], a[i+1]));
        }
    }

But as you can probably see I'm pretty clueless and I'm sure my attempt is wrong! Any help would be great. I have looked at comparator's online but they just seem to be for a specific data type whereas this is for any non-primitive element type.

Upvotes: 0

Views: 2383

Answers (5)

Anand Balakrishnan
Anand Balakrishnan

Reputation: 21

Try this:

public static T greatest(T[] a, Comparator<T> x) {
    T temp = a[0];
    for (int i = 0; i < a.length;i++) {
        if (x.compare(temp,a[i]) <= 0) {
            temp = a[i];
        }
    }
    return temp;
}

Upvotes: 0

Martin M J
Martin M J

Reputation: 880

Compare<T> is a generic interface, as seen by the parameter <T>. T can be any class. Because the below method takes an argument of type Comparator<T>, it can take any comparator that implements this interface.

Note that the array holds objects of type T. This is necessary, because the Comparator only knows how to compare objects of this type.

public static <T> T greatest(T[] a, Comparator<? super T> x) {
    T greatest = a[0];
    for (int i = 1; i < a.length; i++) {
        if (x.compare(a[i], greatest) > 0) {
            greatest = a[i];
        }
    }
    return greatest;
}

Upvotes: 2

Tesseract
Tesseract

Reputation: 8139

Your method is supposed to be generic and return a result. Here is part of the solution, you just have to add a few things to make it complete.

public static <T> T greatest(T[] a, Comparator<? super T> comp) {
    T greatest = a[0];
    for(int i = 1; i < a.length; i++) {
    }
    return greatest;
}

Upvotes: 1

eckes
eckes

Reputation: 10423

The method must be generic (in the text the "object" is used missleading). It also needs to return the result (in this case an object of the input type, but some solutions might instead return the index of the object).

So the function declaration will look like:

public static <T> T greatest(T[] objects, Comparator<T> comparator)

And then you need to find the biggest one. This is simply done by remembering the biggest one you have seen so far. As soon as a element was larger, remember the new one:

{
    assert objects.length >= 1;
    T pivot = objects[0]; // assume the first is biggest
    for(T o : objects) // no good way to start with second
    {
        if (comp.compare(o, pivot) > 0) // if o is bigger
            pivot = o;
    }
    return pivot;
}

The code is uncompiled and untested.

Upvotes: 0

Manuel Jain
Manuel Jain

Reputation: 751

  1. initialize the current maximum with the first element a[0]
  2. iterate over the array and compare the current element with the maximum
  3. if current element is greater then maximum, it is the new maximum
  4. return the maximum

Upvotes: 1

Related Questions