gpt4 book ai didi

java - 我正在尝试按字典顺序对矩形进行排序。 (x,y,宽度,高度)

转载 作者:行者123 更新时间:2023-11-30 04:00:29 24 4
gpt4 key购买 nike

如果矩形的 x 值相同,则比较 y 值,然后检查宽度和高度等。

public class RectangleTester
{
public static void main(String[] args)
{
ArrayList<Rectangle2D> list = new ArrayList<>();
list.add(new Rectangle2D.Double(100,100,50,50));
list.add(new Rectangle2D.Double(120,80,54,75));
list.add(new Rectangle2D.Double(85,95,60,55));
list.add(new Rectangle2D.Double(100,95,55,55));
list.add(new Rectangle2D.Double(100,95,55,52));

System.out.println("\nUnsorted Rectangles:");
for (Rectangle2D temp : list)
System.out.println(temp.toString() + " Y: " + temp.getY());

这就是我逻辑困惑的地方。我不知道为什么在比较具有相同 x 值或 y 值的 2 个矩形时,比较器没有正确列出正确的顺序。

Comparator<Rectangle2D> comp = new
Comparator<Rectangle2D>()
{
public int compare(Rectangle2D r1, Rectangle2D r2)
{
Double r1X = r1.getX();
Double r2X = r2.getX();
Double r1Y = r1.getY();
Double r2Y = r2.getY();
Double r1Width = r1.getWidth();
Double r2Width = r2.getWidth();
Double r1Height = r1.getHeight();
Double r2Height = r2.getHeight();

if (r1X == r2X && r1Y == r2Y && r1Width == r2Width && r1Height != r2Height)
return r1Height.compareTo(r2Height);
else if (r1X == r2X && r1Y == r2Y && r1Width != r2Width)
return r1Width.compareTo(r2Width);
else if (r1X == r2X && r1Y != r2Y)
return r1Y.compareTo(r2Y);
else
return r1X.compareTo(r2X);
}
};

Collections.sort(list, comp);

System.out.println("\nSorted Rectangles:");
for (Rectangle2D temp : list)
System.out.println(temp.toString());
}
}

我的输出显示如下:

Unsorted Rectangles:

java.awt.geom.Rectangle2D$Double[x=100.0,y=100.0,w=50.0,h=50.0] Y: 100.0
java.awt.geom.Rectangle2D$Double[x=120.0,y=80.0,w=54.0,h=75.0] Y: 80.0
java.awt.geom.Rectangle2D$Double[x=85.0,y=95.0,w=60.0,h=55.0] Y: 95.0
java.awt.geom.Rectangle2D$Double[x=100.0,y=95.0,w=55.0,h=55.0] Y: 95.0
java.awt.geom.Rectangle2D$Double[x=100.0,y=95.0,w=55.0,h=52.0] Y: 95.0

Sorted Rectangles:

java.awt.geom.Rectangle2D$Double[x=85.0,y=95.0,w=60.0,h=55.0]
java.awt.geom.Rectangle2D$Double[x=100.0,y=100.0,w=50.0,h=50.0]
java.awt.geom.Rectangle2D$Double[x=100.0,y=95.0,w=55.0,h=55.0]
java.awt.geom.Rectangle2D$Double[x=100.0,y=95.0,w=55.0,h=52.0]
java.awt.geom.Rectangle2D$Double[x=120.0,y=80.0,w=54.0,h=75.0]

如您所见,矩形已根据 x 值正确排序,但当多个矩形具有相同的 x 值时,排序无法将它们按正确的顺序排列。我对 if 语句的逻辑感到抓狂,我只需要另一双眼睛。

最佳答案

第一个错误,您正在将对象与 == 进行比较。 == 比较引用,而不是值。

第二个错误,你的逻辑不正确,而且不必要的复杂。只需按照您所描述的方式实现即可:如果矩形具有相同的 x 值,则比较 y 值:

int result = r1X.compareTo(r2X);
if (result == 0) {
result = r1Y.compareTo(r2Y);
}
if (result == 0) {
result = r1Width.compareTo(r2Width);
}
if (result == 0) {
result = r1Height.compareTo(r2Height);
}
return result;

使用Guava,可以轻松实现

return ComparisonChain.start()
.compare(r1X, r2X)
.compare(r1Y, r2Y)
.compare(r1Width, r2Width)
.compare(r1Height, r2Height)
.result();

使用 Java 8,它会更加干净:

Comparator<Rectangle> comparator = 
Comparator.comparing(Rectangle::getX)
.thenComparing(Rectangle::getY)
.thenComparing(Rectangle::getWidth)
.thenComparing(Rectangle::getHeight)

关于java - 我正在尝试按字典顺序对矩形进行排序。 (x,y,宽度,高度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22120968/

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