Comparator collections.binarySearch()无法与Compariable一起使用

Comparator collections.binarySearch()无法与Compariable一起使用,comparator,comparable,Comparator,Comparable,在按升序和降序对列表进行排序之后,我想调用binarySearch方法。 但它并没有像我所实现的那样工作 class Student implements Comparable<Student>{ private int id; private String name; public Student(int id, String name){ this.id = id; this.name = name; } public int getId(){ re

在按升序和降序对列表进行排序之后,我想调用binarySearch方法。 但它并没有像我所实现的那样工作

 class Student implements Comparable<Student>{
private int id;
private String name;

public Student(int id, String name){
    this.id = id;
    this.name = name;
}

public int getId(){
    return id;
}
public String getName(){
    return name;
}

@Override
public int compareTo(Student o) {

    int j = o.getId();
    int result = this.id - j;
    return result;
}


}
public class CollectionSearchDemo {


public static void main(String[] args) {

    List<Student> list = new ArrayList<Student>();
    list.add(new Student(3, "ouier"));
    list.add(new Student(2, "fdgds"));
    list.add(new Student(7, "kiluf"));
    list.add(new Student(1, "6trfd"));
    list.add(new Student(8, "hjgas"));
    list.add(new Student(5, "ewwew"));

    Collections.sort(list, new Comparator<Student>() {

        @Override
        public int compare(Student arg0, Student arg1) {

            return arg0.getId() - arg1.getId();
        }
    });

    Iterator iterator = list.iterator();
    while(iterator.hasNext()){
        Student student = (Student) iterator.next();
        System.out.print(student.getId()+":"+student.getName()+" ");
    }

    System.out.println("\nSorting in reverse order:");

//  Collections.reverse(list);
    Comparator<Student> collections = Collections.reverseOrder();
    Collections.sort(list, collections);

    Iterator iterator1 = list.iterator();
    while(iterator1.hasNext()){
        Student student = (Student) iterator1.next();
        System.out.print(student.getId()+":"+student.getName()+" ");
    }

    System.out.println("I want to do searching ");
    System.out.println("\n2 is at:"+Collections.binarySearch(list, 2, new Student()));
            // facing exception at this line.I don't know what to use as argument of                           binarySearch() method. 
}

}
班级学生实施可比较的{
私有int-id;
私有字符串名称;
公立学生(整数id,字符串名称){
this.id=id;
this.name=名称;
}
公共int getId(){
返回id;
}
公共字符串getName(){
返回名称;
}
@凌驾
公共内部比较(学生o){
int j=o.getId();
int result=this.id-j;
返回结果;
}
}
公共类集合searchdemo{
公共静态void main(字符串[]args){
列表=新的ArrayList();
列表。添加(新学生(3,“ouier”);
增加(新学生(2名,“fdgds”);
增加(新学生(7名,kiluf));
列表。添加(新学生(1,“6trfd”);
增加(新学生(8名,“hjgas”);
列表。添加(新学生(5,“EWEWW”);
Collections.sort(list,newcomparator(){
@凌驾
公共整数比较(学生arg0,学生arg1){
返回arg0.getId()-arg1.getId();
}
});
迭代器迭代器=list.Iterator();
while(iterator.hasNext()){
学生=(学生)迭代器。下一步();
System.out.print(student.getId()+“:“+student.getName()+”);
}
System.out.println(“\n按相反顺序排序:”);
//收款。反向(列表);
Comparator collections=collections.reverseOrder();
集合。排序(列表、集合);
迭代器迭代器1=list.Iterator();
while(iterator1.hasNext()){
学生=(学生)迭代器1.next();
System.out.print(student.getId()+“:“+student.getName()+”);
}
System.out.println(“我想做搜索”);
System.out.println(“\n2位于:”+Collections.binarySearch(list,2,newstudent());
//此行遇到异常。我不知道用什么作为binarySearch()方法的参数。
}
}
我可以通过实现comparator来实现这一点,但我的项目中有这样的要求
请指导我。

来自收藏文档。binarySearch:

在进行此调用之前,必须根据其元素的自然顺序(如通过排序(list)方法)将列表按升序排序。如果未排序,则结果未定义


您已删除方法调用的结尾:
Collections.binary