-3

给定 Java 中的三个类 X、Y 和 Z。

在 XI 中有一个公共的非静态函数“double distance(Y)”。在 ZI 中有一个 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;
  }
};

边是 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)
4

1 回答 1

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);
于 2013-04-24T12:45:24.527 回答