Java Arrays.sort(pointsBySlope,p.SLOPE_ORDER)是否取决于pointsBySlope[]是什么?
Point是一个类,它有两个实例变量,它们是点的坐标。(x,y) 点实现可比较的接口。但是,除了通过坐标进行比较外,任何点(x1,y1)也可以与(x2,y2)进行比较,它们相对于另一点(x0,y0)的相对斜率。以下是Point类:Java Arrays.sort(pointsBySlope,p.SLOPE_ORDER)是否取决于pointsBySlope[]是什么?,java,sorting,Java,Sorting,Point是一个类,它有两个实例变量,它们是点的坐标。(x,y) 点实现可比较的接口。但是,除了通过坐标进行比较外,任何点(x1,y1)也可以与(x2,y2)进行比较,它们相对于另一点(x0,y0)的相对斜率。以下是Point类: import java.util.Comparator; public class Point implements Comparable<Point> { // compare points by slope public final Compara
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;
}
}
}
为什么两种情况下的输出不同?当直接按斜率顺序排序时,最初按自然顺序对点的斜率数组排序,然后按斜率顺序排序,如何产生不同的输出?在这两种排序过程中,
p
是(19000,10000)
。然后,每个(14000,10000)、(18000,10000)、(21000,10000)、(32000,10000)
都有一个0
的“斜率到p
”。这意味着您的比较器不会对这些点重新排序;它只会让他们按照排序之前的顺序排列。对于第二个输出,这恰好是点的自然顺序;对于第一个输出,它不是。通过使用调试器逐步完成这一过程,您学到了什么?
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) ,