Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我可以调用binarySearch方法而不实现comparator/comparable吗?_Java_Collections - Fatal编程技术网

Java 我可以调用binarySearch方法而不实现comparator/comparable吗?

Java 我可以调用binarySearch方法而不实现comparator/comparable吗?,java,collections,Java,Collections,如果我想调用binarySearch()方法来执行搜索操作,是否需要实现comparator或comparable 我在尝试调用binarySearch()方法时遇到异常 import java.util.ArrayList; 导入java.util.Collections; 导入java.util.Comparator; 导入java.util.Iterator; 导入java.util.List; 班级学生用具{ 私有int-id; 私有字符串名称; 公立学生(整数id,字符串名称){ th

如果我想调用binarySearch()方法来执行搜索操作,是否需要实现comparator或comparable 我在尝试调用binarySearch()方法时遇到异常

import java.util.ArrayList;
导入java.util.Collections;
导入java.util.Comparator;
导入java.util.Iterator;
导入java.util.List;
班级学生用具{
私有int-id;
私有字符串名称;
公立学生(整数id,字符串名称){
this.id=id;
this.name=名称;
}
公立学生(){
//TODO自动生成的构造函数存根
}
公共int getId(){
返回id;
}
公共字符串getName(){
返回名称;
}
}
公共类集合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(“我想做搜索”);
System.out.println(“\n2位于:”+Collections.binarySearch(list,2,newstudent());
//调用binarySearch方法时遇到异常。
}
}
尝试搜索“new Student()”或“CollectionSearchDemo”作为参数时发生异常。 我不知道在binraySearch方法中应该传递什么作为参数

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

class Student implements{
private int id;
private String name;

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

public Student() {
    // TODO Auto-generated constructor stub
}

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

}
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("I want to do searching ");
    System.out.println("\n2 is at:"+Collections.binarySearch(list, 2, new Student()));
            // facing exception when i invoke binarySearch method.
}
}

请帮助我

binarySearch方法的第三个参数必须是Comparator,引发异常是因为您的学生没有实现Comparator接口

binarySearch(列表、对象键、比较器c)

请看这里:


参数c必须是实现Comparator的类的实例。

binarySearch方法的第三个参数必须是Comparator,因为您的学生没有实现Comparator接口,所以引发异常

binarySearch(列表、对象键、比较器c)

请看这里:


参数c必须是实现Comparator的类的实例。

简短的回答是“是”-二进制搜索不知道您搜索的
学生是“更大”还是“更小”,因此无法搜索。
您希望使用比较器中已有的代码在
学生
上实现Compariable:

public class Student implements Comparable<Student> {
//stuff
 @Override
 public int compareTo(Student o) {
  return getId() - o.getId();
 }
}
公共班级学生实施可比{
//东西
@凌驾
公共内部比较(学生o){
返回getId()-o.getId();
}
}
然后,您使用的代码如下所示:

final Set<Student> set = new TreeSet<Student>();
//add students etc...
final Student found = Collections.binarySearch(set, studentToLookFor);
final Set=new TreeSet();
//添加学生等。。。
最终找到的学生=Collections.binarySearch(set,studentToLookFor);

简短的回答是肯定的-二进制搜索不知道您要搜索的学生是“大”还是“小”,因此无法搜索。
您希望使用比较器中已有的代码在
学生
上实现Compariable:

public class Student implements Comparable<Student> {
//stuff
 @Override
 public int compareTo(Student o) {
  return getId() - o.getId();
 }
}
公共班级学生实施可比{
//东西
@凌驾
公共内部比较(学生o){
返回getId()-o.getId();
}
}
然后,您使用的代码如下所示:

final Set<Student> set = new TreeSet<Student>();
//add students etc...
final Student found = Collections.binarySearch(set, studentToLookFor);
final Set=new TreeSet();
//添加学生等。。。
最终找到的学生=Collections.binarySearch(set,studentToLookFor);

使用集合有两种方法。binarySearch
。第一个元素只有一个元素列表和一个键,但这些元素必须实现
Comparable
。第二种方法是使用键和比较器。如果您不希望
学生
实现
可比
,则必须使用此选项

所以你必须写一些类似的东西:

 Collections.binarySearch(list, studentToBeFound, comparator);
studenttobeforound
是一个
Student
实例,比较器是一个
comparator
,就像您在
Collection.sort()中使用的那样

只需重复使用以前与
Id
进行比较的比较器:

private static final class StudentComparator implements Comparator<Student> {
  @Override
  public int compare(Student arg0, Student arg1) {

    return arg0.getId() - arg1.getId();
  }
}
并将其与binarySearch一起使用:

int indexInList = Collections.binarySearch(list, studentToBeFound, new StudentComparator());

有两种方法可以使用集合。binarySearch
。第一个元素只有一个元素列表和一个键,但这些元素必须实现
Comparable
。第二种方法是使用键和比较器。如果您不希望
学生
实现
可比
,则必须使用此选项

所以你必须写一些类似的东西:

 Collections.binarySearch(list, studentToBeFound, comparator);
studenttobeforound
是一个
Student
实例,比较器是一个
comparator
,就像您在
Collection.sort()中使用的那样

只需重复使用以前与
Id
进行比较的比较器:

private static final class StudentComparator implements Comparator<Student> {
  @Override
  public int compare(Student arg0, Student arg1) {

    return arg0.getId() - arg1.getId();
  }
}
并将其与binarySearch一起使用:

int indexInList = Collections.binarySearch(list, studentToBeFound, new StudentComparator());

如果我们不知道如何排序,列表将无法排序。二进制搜索算法要求对列表进行排序,它使用列表的顺序来搜索列表,并在每次迭代中将搜索字段对半。但是学生x是在列表的前半部分还是后半部分的概念要求我们知道这个列表是如何排序的

选项1:
公共班级学生实施可比的

选项2:为学生编写一个比较类


公共类StudentComparator实现了Comparator

如果我们不知道如何对列表排序,则无法对其排序。二进制搜索算法要求对列表进行排序,它使用列表的顺序来搜索列表,并在hal中剪切搜索字段