gpt4 book ai didi

java - Arrays.sort(pointsBySlope, p.SLOPE_ORDER) 是否取决于pointsBySlope[] 是什么?

转载 作者:行者123 更新时间:2023-12-01 12:44:28 25 4
gpt4 key购买 nike

Point 是一个类,它有两个实例变量,它们是点的坐标。 (x,y)Point 实现了 Comparable 接口(interface)。但除了通过坐标进行比较之外,任何点 (x1,y1) 还可以与 (x2,y2) 进行比较,即它们相对于另一个点 (x0,y0) 的相对斜率。以下是 Point 类:

import java.util.Comparator;

public class Point implements Comparable<Point> {

// compare points by slope
public final Comparator<Point> SLOPE_ORDER = new Ordering();
private final int x; // x coordinate
private final int y; // y coordinate

// create the point (x, y)
public Point(int x, int y) {
/* DO NOT MODIFY */
this.x = x;
this.y = y;
}

private class Ordering implements Comparator<Point>{

@Override
public int compare(Point p1, Point p2) {
if(slopeTo(p1) - slopeTo(p2) < 0) return -1;
else if(slopeTo(p1) - slopeTo(p2) > 0) return 1;
else return 0;
}

}

// slope between this point and that point
public double slopeTo(Point that) {
/* YOUR CODE HERE */
if(this.y == that.y && this.x == that.x) return Double.NEGATIVE_INFINITY;
else if(this.y == that.y) return 0;
else if(this.x == that.x) return Double.POSITIVE_INFINITY;
else return ((double)that.y - (double)this.y)/((double)that.x - (double)this.x);
}

// is this point lexicographically smaller than that one?
// comparing y-coordinates and breaking ties by x-coordinates
public int compareTo(Point that) {
/* YOUR CODE HERE */
if(this.y < that.y){
return -1;
}else if(this.y > that.y){
return 1;
}else{
if(this.x < that.x) return -1;
else if(this.x > that.x) return 1;
else return 0;
}
}
}

现在我编写了一个简单的测试类来测试按斜率排序是否有效。

public class Test {

public static void main(String[] args) {
In in = new In(args[0]);
int N = in.readInt();
Point[] points = new Point[N];
Point[] pointsBySlope = new Point[N];
for (int i = 0; i < N; i++) {
int x = in.readInt();
int y = in.readInt();
points[i] = new Point(x, y);
pointsBySlope[i] = points[i];
}
Arrays.sort(points);
Arrays.sort(pointsBySlope);

Point p = points[2];
Arrays.sort(pointsBySlope, p.SLOPE_ORDER);

p = points[3];
Arrays.sort(pointsBySlope, p.SLOPE_ORDER);
for(int i=0;i<pointsBySlope.length;i++){
System.out.println(pointsBySlope[i].toString()+" , ");
}
System.out.println("\n\n"+"=================================");
Arrays.sort(pointsBySlope);
Arrays.sort(pointsBySlope, p.SLOPE_ORDER);
for(int i=0;i<pointsBySlope.length;i++){
System.out.println(pointsBySlope[i].toString()+" , ");
}
}

}

输出为

(19000, 10000) , 
(18000, 10000) ,
(14000, 10000) ,
(21000, 10000) ,
(32000, 10000) ,
(1234, 5678) ,

(19000, 10000) ,
(14000, 10000) ,
(18000, 10000) ,
(21000, 10000) ,
(32000, 10000) ,
(1234, 5678) ,

为什么两种情况下的输出不同?最初按自然顺序对 pointsBySlope 数组进行排序,然后按 SLOPE_ORDER 排序,当直接按 SLOPE_ORDER 排序时,如何产生不同的输出?

最佳答案

在这两种排序中,p 都是 (19000, 10000)。然后,每个 (14000, 10000)、(18000, 10000)、(21000, 10000)、(32000, 10000) 的“到 p 的斜率”为 0。这意味着您的比较器不会对这些点重新排序;它只会让它们保持排序之前的顺序。对于你的第二个输出,这恰好是点的自然顺序;对于第一个输出,它不是。

关于java - Arrays.sort(pointsBySlope, p.SLOPE_ORDER) 是否取决于pointsBySlope[] 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24829635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com