2013-04-24 61 views
-3

給出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) 
+2

說明OK。 。代碼是哪裏? – 2013-04-24 12:38:43

+0

對不起,我沒有意識到我應該發佈代碼。這個問題很簡單,代碼很困惑,可能在概念上是錯誤的... – user2315499 2013-04-25 01:52:06

回答

3

Z您可以創建一個比較,看起來是這樣的:

public static Comparator<Y> cmp = new Comparator<Y>() { 
    public int compare(Y y1, Y y2) { 
     return (int) Math.signum(x.distance(y1) - x.distance(y2)); 
    } 
}; 

然後,您可以使用此比較來排序您的數組:

Arrays.sort(arrayOfY, cmp); 
+0

距離返回double,比較返回int,你會得到類型不匹配 – user902383 2013-04-24 12:48:56

+0

是的,你是對的!我編輯了我的答案來解決這個問題。 – ValarDohaeris 2013-04-24 13:44:30

+0

+1進行編輯,順便說一句,你不需要在那裏做任何數學,你可以做Double.compare(x.distance(y1),x.distance(y2)) – user902383 2013-04-24 14:00:41