Java で 3 つのクラス X、Y、Z が与えられます。
XI にはパブリックな非静的関数 "double distance(Y)" があります。ZI には、X 型のオブジェクト x と配列があります。x からの distance() の昇順で配列を並べ替えるにはどうすればよいですか?
Y のコンパレータを使用して解決策を見つけようとしていますが、無知であることを認めなければなりません。ありがとうございました!
編集:詳細を見つけることができなかったエラーが引き続き発生します。回答の解決策に基づいて、私のコードは次のようになります。
public SARAgent(PathBandit game, int n) {
...
Arrays.sort(getEdgeSet(game).toArray(), BY_GAP);
}
public static Comparator<Edge> BY_GAP = new Comparator<Edge>() {
public int compare(Edge a, Edge b) {
double va = game.delta(a.getX(), a.getY(), a.getDir());
double vb = game.delta(b.getX(), b.getY(), b.getDir());
if (va == vb) return a.compareTo(b);
if (va < vb) return -1;
if (va > vb) return +1;
return 0;
}
};
Edges は Comparable ですが、Arrays.sort() の呼び出しでエラーが発生し、コードはコンパイルされません。エラーは
Error: no suitable method found for sort(java.lang.Object[],java.util.Comparator<Edge>)
method java.util.Arrays.<T>sort(T[],int,int,java.util.Comparator<? super T>) is not applicable
(cannot instantiate from arguments because actual and formal argument lists differ in length)
method java.util.Arrays.<T>sort(T[],java.util.Comparator<? super T>) is not applicable
(actual argument java.util.Comparator<Edge> cannot be converted to java.util.Comparator<? super java.lang.Object> by method invocation conversion)
method java.util.Arrays.sort(java.lang.Object[],int,int) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(java.lang.Object[]) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(double[],int,int) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(double[]) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(float[],int,int) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(float[]) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(byte[],int,int) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(byte[]) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(char[],int,int) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(char[]) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(short[],int,int) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(short[]) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(long[],int,int) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(long[]) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(int[],int,int) is not applicable
(actual and formal argument lists differ in length)
method java.util.Arrays.sort(int[]) is not applicable
(actual and formal argument lists differ in length)