gpt4 book ai didi

java - 有谁知道如何使用 array.sort() 对堆栈进行排序?我只想按 x 坐标打印点的有序列表。任何想法?

转载 作者:行者123 更新时间:2023-11-30 09:47:42 25 4
gpt4 key购买 nike

import java.util.*;

public class Point2D
{
private double x;
private double y;

public Point2D(double x, double y)
{
this.x = x;
this.y = y;
}

private double Euclidean()
{
double Distance = Math.sqrt((x * x) + (y * y));
return Distance;
}

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

public static class YOrder implements Comparator<Point2D>
{
public int compare(Point2D self, Point2D other)
{
if (self.y > other.y)
return 1;

if (self.y < other.y)
return -1;

else
return 0;
}
}

public static void main(String[] args)
{
Stack<Point2D> stack = new Stack<Point2D>();

while (!StdIn.isEmpty())
{
double x = StdIn.readDouble();
double y = StdIn.readDouble();
stack.push(new Point2D(x,y));
}

Arrays.sort(stack);
}
}

这是一个类,我不允许使用 CollectionsArrayList

最佳答案

Arrays.sort(stack);

应该是

 Collections.sort(stack, new YOrder());//pass comparator

关于java - 有谁知道如何使用 array.sort() 对堆栈进行排序?我只想按 x 坐标打印点的有序列表。任何想法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6554839/

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