給出Java中的三個類X,Y和Z.java在另一個類中的類的比較器
在X我有一個公共非靜態函數「雙距離(Y)」。 在Z中,我有一個X類型的對象x和一個數組。 我怎麼能按距離()從x的升序排列數組?
我想找到一個解決方案使用比較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;
}
};
邊緣相媲美,但代碼不能編譯,有錯誤就調用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)
說明OK。 。代碼是哪裏? – 2013-04-24 12:38:43
對不起,我沒有意識到我應該發佈代碼。這個問題很簡單,代碼很困惑,可能在概念上是錯誤的... – user2315499 2013-04-25 01:52:06