【问题标题】:How To Sort Through An array from type "NameOfClass"如何对“NameOfClass”类型的数组进行排序
【发布时间】:2017-07-03 04:13:06
【问题描述】:

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

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和Point 1之间的距离最小所以程序应该输出:1,1 2,2 6,6 6,6 会排在最后的原因是因为 6,6 和 2,2 之间的距离最大,我是 Java 新手,所以如果你能向我解释一下,那将是一个很大的帮助。

【问题讨论】:

  • 我不确定您的意思,您能否进一步澄清一下。 “彼此之间的距离”是什么意思?您的意思是要计算到所有点的最大距离,然后对结果进行排序?
  • 没错,就是我的意思

标签: java arrays eclipse class


【解决方案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 +
                '}';
    }
}

【讨论】:

  • 有没有办法不添加方法而只在程序类中使用循环?
  • @MicaelIllos 为什么要这样做?你在一个方法/类中投入的越多,它就越难阅读。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-20
  • 1970-01-01
  • 2020-05-29
  • 2020-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多