Collections.sort()错误
我试图根据类A的int排序类B中名为BinOrder的类型A的列表。
但是我收到此错误行Collections.sort(BinOrder);
The method sort(List<T>) in the type Collections is not applicable for the arguments (ArrayList<A>)
A类:
public class A{int s;
int r;
public A(int si, int ri) {
s=si;
r= ri;
}
}
B级:
import java.util.ArrayList;import java.util.Collections;
public class B implements Comparable<A> {
public Iterator<A> randomMethodName(int a) {
ArrayList<A> BinOrder = new ArrayList<A>();
A a = new A(1,3)
A a2 = new A(1,4)
BinOrder.add(a);
BinOrder.add(a2);
}
// sort array in increasing order of r
Collections.sort(BinOrder);
return BinOrder;
}
@Override
public int compareTo(A list) {
return null;
}
}
回答:
为了能够使用单参数版本Collection.sort()
上ArrayList
的A
,A
应该实现的Comparable
接口:
public class A implements Comparable<A> { ...
@Override
int compareTo(A rhs) {
...
}
}
以上是 Collections.sort()错误 的全部内容, 来源链接: utcz.com/qa/412386.html