简体   繁体   English

java Collections.binarySearch()返回-2

[英]java Collections.binarySearch() returning -2

I have a text file which contains student information. 我有一个包含学生信息的文本文件。 I sort this information using the comparator class which implements the comparator interface.This sorts fine fine when calling Collection.Sort(); 我使用实现comparator接口的比较器类对这些信息进行排序。这在调用Collection.Sort();时排序很好Collection.Sort();

I then use the comparator in the binarySearch() method. 然后我在binarySearch()方法中使用comparator I have an int index that returns the index of the binary search. 我有一个int索引,它返回二进制搜索的索引。 after debugging it seems that the binarySearch() isnt sorting the data properly because I get an index of 0 for 501 which is correct, an index of 1 for 503 which is incorrect and an index of -2 for 502. Is the binarySearch not using the comparator apppropriately and thus not sorting the data appropriately as its returning the data in its original form. 在调试之后,似乎binarySearch()正确地对数据进行排序,因为我得到的索引为0表示501是正确的,索引为1表示503表示不正确,索引表示为-2表示502. binarySearch不使用比较器适当地因此不能正确地对数据进行排序,因为它以原始形式返回数据。

 =================================================
    First Name: Mark
    Last Name: Evans
    Registration: 501

    Subject: Maths
    Assignment: 1
    Homewok Mark: 70
    Exam Mark: 80

    Assignment: 2
    Homewok Mark: 70
    Exam Mark: 40

    Subject: English
    Assignment: 1
    Homewok Mark: 40
    Exam Mark: 50

    Assignment: 2
    Homewok Mark: 60
    Exam Mark: 70

    Subject: Science
    Assignment: 1
    Homewok Mark: 50
    Exam Mark: 60

    Assignment: 2
    Homewok Mark: 80
    Exam Mark: 45

    Assignment: 3
    Homewok Mark: 67
    Exam Mark: 47

    Assignment: 4
    Homewok Mark: 38
    Exam Mark: 57

    =================================================
    First Name: Linda
    Last Name: Evans
    Registration: 503

    Subject: Maths
    Assignment: 1
    Homewok Mark: 50
    Exam Mark: 60

    Assignment: 2
    Homewok Mark: 70
    Exam Mark: 50

    =================================================
    First Name: Joseph
    Last Name: Evanw
    Registration: 502

    Subject: English
    Assignment: 1
    Homewok Mark: 40
    Exam Mark: 50

I have a comparator class that works fine and sorts the data appropriately. 我有一个工作正常的比较器类,并适当地排序数据。

public class StudentComparator implements Comparator<Student> {

    @Override
    public int compare(Student s1, Student s2) {

        int registrationNumber1 = s1.getRegistrationNumber();
        int registrationNumber12 = s2.getRegistrationNumber();

        return registrationNumber1 - registrationNumber12;
    }

}


public void searchStudentByName() {

        int number = 503;
        int subjectNumber = 1;

        int index = Collections.binarySearch(studentList, new Student(number),
                new StudentComparator());
        for (int i = 0; studentList.size() > i; i++) {
            if (i == index) {

                System.out.print(studentList.get(i));

            }
        }

    }

To perform a binary search, the collection must already be sorted. 要执行二进制搜索,必须已对该集合进行排序。 You are expecting binarySearch to sort it for you, but it doesn't sort it. 您期望binarySearch为您排序,但它不会对它进行排序。 It takes advantage of the assumption that it's already sorted. 它利用了它已经排序的假设。 The Javadocs for Collections.binarySearch state: Collections.binarySearch状态的Javadocs

Searches the specified list for the specified object using the binary search algorithm. 使用二进制搜索算法在指定列表中搜索指定的对象。 The list must be sorted into ascending order according to the specified comparator (as by the sort(List, Comparator) method), prior to making this call. 在进行此调用之前,必须根据指定的比较器(通过sort(List,Comparator)方法)将列表按升序排序。 If it is not sorted, the results are undefined. 如果未排序,则结果未定义。

Add a call to Collections.sort before attempting a binary search. 在尝试二进制搜索之前添加对Collections.sort的调用。

StudentComparator sc = new StudentComparator();
Collections.sort(studentList, sc);

Then you can call Collections.binarySearch . 然后你可以调用Collections.binarySearch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM