I'm trying to create a method which in turn should call Arrays.sort method and I'm trying this method to be generic itself. The problem comes when I try to create a concrete Comparator but I always get one exception like:
Generics.java:15: cannot find symbol
symbol : method sort(java.lang.Comparable[],java.util.Comparator<T>)
location: class java.util.Arrays
Arrays.sort(array, c);
^
1 error
I workaround it by casting the value, but I would really like to know how to code it properly.
Here's my ( relevant ) code:
import java.util.*;
abstract class Generics<T1,T2> {
public abstract void run( T1 a , T2 b );
public static <T> void sort( Comparable[] array, final Generics<T,T> g ) {
Comparator<T> c = new Comparator<T>() {
public int compare(T o1, T o2) {
g.run(o1,o2);
return 1;
}
};
Arrays.sort(array,
/* how can I get rid of this cast? */
(Comparator<? super Comparable>)
/* and use only */ c);
}
public static void main( String ... args ) {
Generics.sort( args, new Generics<String,String>() {
public void run( String a, String b ) {
System.out.println( "Irrelevant");
}
});
}
}
return 1) does not fulfill the contract of the interface (antisymmetry, reflexivity). I suppose this is only for testing purposes.