如何排序“NameOfClass”类型的数组

时间:2017-02-14 18:19:46

标签: java arrays eclipse class

我有一个程序可以找到一对点之间的最大距离: 这是类Point:

public class Point {

  private int x;
  private  int y;
  public Point (int a, int b) {
      x=a; y=b;
  }
  public double distance (Point p){
    int tempx , tempy;
    tempx=(x-p.x)*(x-p.x);
    tempy=(y-p.y)*(y-p.y);
    return  Math.sqrt(tempx+tempy);
  }
  public void show(){
      System.out.println("(" + x + "," + y + ")");
  }
}

这是主要计划:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Point[] allPoints=new Point[5];
      int k=0;
      int rx,ry;
      while (k<=allPoints.length-1){             
      rx=(int)(1+Math.random()*10);               
      ry=(int)(1+Math.random()*10);
      allPoints[k]=new Point(rx, ry);
      k++;
      }

      int i,j,mi,mj;
      double mDis=0,cDis;
      mi=mj=0;
      i=0;
      while ( i<allPoints.length-1) {
        j=i+1;
        while (j<allPoints.length) {
          cDis= allPoints[i].distance(allPoints[j]);
          if (cDis>mDis){
          mDis=cDis; 
          mj=j; 
          mi=i;}
          j++;
         }    
         i++;
        }

      allPoints[mj].show();
      allPoints[mi].show();
        System.out.print("max dis is " + mDis +" first point: " + mi + " second point: " + mj);
} 
      //////////


}

正如您所看到的,该程序有一个包含5个“单元格”的数组,并且找到了具有最大距离的对,我想知道如何更改程序以便根据距离对程序进行排序和打印它们之间。 例如:Point1:2,2 Point2:6,6 Point3:1,1 所以Point3和Point1之间的距离最小,所以程序应输出:1,1 2,2 6,6 之所以6,6会是最后一次是因为6,6和2,2之间的距离是最大的,我是Java的新手,所以如果你能向我解释它将是一个很大的帮助。

1 个答案:

答案 0 :(得分:1)

创建一个消耗一对点的新Line对象。将所有点组合放入​​一个行列表中,并根据这些行的长度(它们之间的距离)对该列表进行排序。

public static void main(String[] args) {
    ...
    List<Line> lines = new ArrayList<>();
    while (i < allPoints.length - 1) {
        ...
        while (j < allPoints.length) {
            lines.add(new Line(allPoints[i], allPoints[j]));
            ...
        }
        ...
    }
    ...
    Collections.sort(lines, Comparator.comparingDouble(Line::getLength));
    System.out.println("Lines: ");
    for (Line line : lines) {
        System.out.println(line);
    }
}

class Point {
    private int x;
    private  int y;

    public Point (int a, int b) {
        x=a; y=b;
    }

    public double distance (Point p){
        int tempx , tempy;
        tempx=(x-p.x)*(x-p.x);
        tempy=(y-p.y)*(y-p.y);
        return  Math.sqrt(tempx+tempy);
    }

    public void show(){
        System.out.println(this);
    }

    @Override
    public String toString() {
        return "(" + x + "," + y + ")";
    }
}

class Line {
    private final Point a;
    private final Point b;
    private double length;

    public Line(Point a, Point b) {
        this.a = a;
        this.b = b;
        this.length = a.distance(b);
    }

    public double getLength() {
        return length;
    }

    @Override
    public String toString() {
        return "Line{" +
                "a=" + a +
                ", b=" + b +
                ", length=" + length +
                '}';
    }
}