我试图根据类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接口:
Collection.sort()
ArrayList
A
Comparable
public class A implements Comparable<A> { ... @Override int compareTo(A rhs) { ... } }