小编典典

Collections.sort()错误

java

我试图根据类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;
    }
}

阅读 563

收藏
2020-11-30

共1个答案

小编典典

为了能够使用单参数版本Collection.sort()ArrayListAA应该实现的Comparable接口:

public class A implements Comparable<A> {
  ...
  @Override
  int compareTo(A rhs) {
    ...
  }
}
2020-11-30