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.