Java Point2D 和类似的

发布于 2024-09-18 22:27:36 字数 932 浏览 7 评论 0原文

我正在制作 Point2D.Float 类型的 TreeSet,并使用实现 Point2D 类型的compare() 的自定义 Comparable 类创建它。但是,在 TreeSet 上调用“contains”时,出现类转换错误: java.lang.ClassCastException: java.awt.geom.Point2D$Float 无法转换为 java.lang.Comparable

是这样创建的:

private CoordinateComparator coordCompare;
public TreeSet<Point2D.Float> coordSet = new TreeSet<Point2D.Float>(coordCompare);

这是我的比较类:

 public class CoordinateComparator implements Comparator<Point2D.Float> {
 public CoordinateComparator() {}

 @Override
 public int compare(Point2D.Float p1, Point2D.Float p2) {
        if (p1.getX() < p2.getX()) 
         return -1;
        if (p1.getX() > p2.getX())
         return 1;
        if (p1.getY() < p2.getY())
         return -1;
        if (p1.getY() > p2.getY())
         return 1;
        return 0;
 }
}

对出了什么问题有什么想法吗?我已经被困在这里几个小时试图调试它但无济于事。谢谢。

I'm making a TreeSet of Point2D.Float type, creating it with a custom Comparable class that implements compare() of Point2D type. However, on calling "contains" on the TreeSet, I get a classcast error: java.lang.ClassCastException: java.awt.geom.Point2D$Float cannot be cast to java.lang.Comparable

The set is created as so:

private CoordinateComparator coordCompare;
public TreeSet<Point2D.Float> coordSet = new TreeSet<Point2D.Float>(coordCompare);

Here is my compare class:

 public class CoordinateComparator implements Comparator<Point2D.Float> {
 public CoordinateComparator() {}

 @Override
 public int compare(Point2D.Float p1, Point2D.Float p2) {
        if (p1.getX() < p2.getX()) 
         return -1;
        if (p1.getX() > p2.getX())
         return 1;
        if (p1.getY() < p2.getY())
         return -1;
        if (p1.getY() > p2.getY())
         return 1;
        return 0;
 }
}

Any ideas on what's going wrong? I've been stuck here for hours trying to debug it to no avail. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无敌元气妹 2024-09-25 22:27:36

添加我之前的评论作为答案。您需要首先初始化比较器。

 public class CoordinateComparator implements Comparator<Point2D.Float> {
 public CoordinateComparator() {}

 @Override
 public int compare(Point2D.Float p1, Point2D.Float p2) {
        if (p1.getX() < p2.getX()) 
         return -1;
        if (p1.getX() > p2.getX())
         return 1;
        if (p1.getY() < p2.getY())
         return -1;
        if (p1.getY() > p2.getY())
         return 1;
        return 0;
 }
}

CoordinateComparator coordCompare = new CoordinateComparator();
TreeSet<Point2D.Float> coordSet = new TreeSet<Point2D.Float>(coordCompare);

@the-alchemist,我猜他得到了 Class Cast Exception 而不是 NPE,因为它试图强制转换为 (Comparator) null 并且由于某种原因在它实际尝试调用之前失败了比较方法。如果没有堆栈跟踪,我不确定。

Adding my previous comment as answer. You need to initialize your Comparator first.

 public class CoordinateComparator implements Comparator<Point2D.Float> {
 public CoordinateComparator() {}

 @Override
 public int compare(Point2D.Float p1, Point2D.Float p2) {
        if (p1.getX() < p2.getX()) 
         return -1;
        if (p1.getX() > p2.getX())
         return 1;
        if (p1.getY() < p2.getY())
         return -1;
        if (p1.getY() > p2.getY())
         return 1;
        return 0;
 }
}

CoordinateComparator coordCompare = new CoordinateComparator();
TreeSet<Point2D.Float> coordSet = new TreeSet<Point2D.Float>(coordCompare);

@the-alchemist, I'm guessing he's getting the Class Cast Exception and not a NPE because it's trying to cast to do (Comparator) null and for some reason that's failing before it actually tries to call the compare method. Without the stack trace I'm not sure though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文