[英]sorting a list of pair of integers in ascending order in java?
I am using the javafx pair class and trying to sort them using the second value. 我正在使用javafx对类,并尝试使用第二个值对它们进行排序。
public static void main(String[] args)
{
List<Pair<Integer, Integer>> dist = new ArrayList<Pair<Integer, Integer>>();
dist.add(new Pair <Integer,Integer> (4,54));
dist.add(new Pair <Integer,Integer> (6,94));
dist.add(new Pair <Integer,Integer> (7,4));
dist.add(new Pair <Integer,Integer> (11,9));
Collections.sort(dist, new ListComparator<>());
System.out.println(dist);
}
public class ListComparator implements Comparator {
@Override
public int compare(Pair<Integer, Integer> o1, Pair<Integer, Integer> o2) {
return o1.getValue() - o2.getValue();
}
}
i keep getting the following errors and warning. 我不断收到以下错误和警告。
/tmp/java_izg9ZN/Main.java:34
: error : cannot infer type arguments forListComparator
Collections.sort(dist, new ListComparator<>());
/tmp/java_izg9ZN/Main.java:34
: 错误 :无法推断ListComparator
Collections.sort(dist, new ListComparator<>());
类型参数Collections.sort(dist, new ListComparator<>());
reason: cannot use '<>
' with non-generic classListComparator
原因:不能对非通用类ListComparator
使用'<>
'
/tmp/java_izg9ZN/ListComparator.java:21
: error :ListComparator
is not abstract and does not override abstract methodcompare(Object,Object)
inComparator
public class ListComparator implements Comparator {
/tmp/java_izg9ZN/ListComparator.java:21
: 错误 :ListComparator
不是 抽象的 ,并且不覆盖Comparator
public class ListComparator implements Comparator {
抽象方法compare(Object,Object)
,public class ListComparator implements Comparator {
/tmp/java_izg9ZN/ListComparator.java:23
: error : method does not override or implement a method from a supertype@Override
/tmp/java_izg9ZN/ListComparator.java:23
: 错误 :方法未 覆盖或通过超类型@Override
实现方法
i have no idea what i am doing wrong because i example i looked at to do this did exactly the same thing so i really dont know what i have done wrong. 我不知道我在做什么错,因为我举个例子,我看着这样做完全一样,所以我真的不知道我做错了什么。 please help! 请帮忙! thanks 谢谢
Your code has two problems . 您的代码有两个问题 。 First you call: 首先您致电:
Collections.sort(dist, new ListComparator<>());
So you tell Java to use a ListComparator
which infers the type by itself (diamond operator <>
). 因此,您告诉Java使用一个ListComparator
,该ListComparator
自己推断类型(菱形运算符<>
)。
However your ListComparator
class is not generic . 但是,您的ListComparator
类不是通用的 。 It does not expect any type arguments, like a String
for example, you don't write String<...>
. 它不期望任何类型参数,例如String
,您不要编写String<...>
。
You could fix this by making ListComparator
generic, working on the elements of the List
for example: 您可以通过使ListComparator
通用化来解决此问题, ListComparator
,处理List
的元素:
public class ListComparator<E> implements Comparator<E> {
// Compare E elements ...
}
However it seems that you rather want a non-generic ListComparator
which is fixed to comparing Pair<Integer, Integer>
elements. 但是,您似乎想要一个非通用的 ListComparator
,它固定用于比较Pair<Integer, Integer>
元素。 In that case remove the diamond operator: 在这种情况下,请删除菱形运算符:
Collections.sort(dist, new ListComparator());
The second problem is that you use raw-types for the Comparator
instead of telling him the type of the objects that will be compared. 第二个问题是,您对Comparator
使用原始类型 ,而不是告诉他将要比较的对象的类型。
Because of that the compiler expects at reading @Override
that your compareTo
methods matches the signature written in Comparator
: 由于编译器预计在读@Override
您compareTo
方法写在签名匹配Comparator
:
public int compareTo(Object o1, Object o2)
And not 并不是
public int compareTo(Pair<Integer, Integer> o1, Pair<Integer, Integer> o2)
You fix this by explicitly telling Comparator
what to use instead of using raw-types : 您可以通过明确告诉Comparator
使用什么而不是使用raw-types来解决此问题:
public class ListComparator implements Comparator<Pair<Integer, Integer>> {
// Compare Pair<Integer, Integer> elements ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.