java另一个类中的类的比较器

时间:2013-04-24 12:38:07

标签: java class comparator

在Java中给出三个类X,Y和Z.

在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)

1 个答案:

答案 0 :(得分: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);