On 9/30/2011 7:03 AM, Fred wrote:
<snip>
Can you please tell me why this code won't _compile_?
import java.util.*;
class InputCounter {
public static void main(String[] args) {
final int MAX_NUMBERS = 50;
int[] array = new int[MAX_NUMBERS];
Arrays.sort(array, Collections.reverseOrder());
}
}
Because the Collections class has no .reverseOrder() method.
It has a .reverseOrder(Comparator) method: You feed it a Comparator
and it returns a new Comparator that inverts the sense of the one
you provided, but it has no method that manufactures a Comparator
out of thin air.
But even if you get over that hurdle it won't do you any good.
A characteristic of Java (some call it a flaw) is that "primitives"
are not objects. In particular, an `int' is not an object: It has
no methods, you can't aim a reference at it, and so on. Among
other things, this means you cannot apply a Comparator to a pair
of `int's, because the Comparator's .compare(Object,Object) method
needs references to two objects. This is why the Arrays class has
distinct methods .sort(int[]), .sort(double[]), .sort(byte[]), and
so on, in addition to .sort(Object[]).
What problem are you trying to solve?