gpt4 book ai didi

java - 设置比较器不是通用的;它不能用参数参数化

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

作为 Java 初学者,我在尝试将自定义比较器分配给集合时遇到以下编译错误:

The type mycode.pointComparator is not generic; it cannot be parameterized with arguments

这是我的代码:

Set<Point3d> cornerPoints = new TreeSet<Point3d>(new pointComparator<Point3d>());

class pointComparator implements Comparator<Point3d> {

@Override
public int compare(Point3d o1, Point3d o2) {
if(!o1.equals(o2)){
return -1;
}
return 0;
}
}

我正在导入Java.util.

更新:删除<Point3d>参数导致此错误:

No enclosing instance of type mycode is accessible. Must qualify the allocation with an enclosing instance of type mycode (e.g. x.new A() where x is an instance of mycode).

最佳答案

这有效:

public static void main(String[] args) throws Exception {
// Look at the following line: diamond operator and new.
Set<Point> points = new TreeSet<>(new PointComparator());
}

static class PointComparator implements Comparator<Point> {

@Override
public int compare(Point p1, Point p2) {
if (!p1.equals(p2)) {
return -1;
}
return 0;
}
}

关于java - 设置比较器不是通用的;它不能用参数参数化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21990714/

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