java - 尝试对对象数组进行二分搜索 [比较器]
问题描述
几天来我一直在努力尝试编写这段代码。基本上,我们必须根据学生数组中可比较的“学生”对象的 SSN 执行二进制搜索。在 SSN 上执行 binarySearch 后,与该 SSN 的名字和姓氏相关联的学生应该打印出来,并且该学生的位置/位置应该打印出来。我遇到的问题是,当我执行 binarySearch 来查找学生的位置/位置时,它总是返回“-1”而不是学生的元素位置。有什么帮助吗?
学生班
package binarySearch;
public class Student implements Comparable<Student>{
private String firstName, lastName, SSN, bankAccount;
public Student(String first, String last, String ssn, String bkacct) {
this.firstName = first;
this.lastName = last;
this.SSN = ssn;
this.bankAccount = bkacct;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getSSN() {
return SSN;
}
public String getBankAccount() {
return bankAccount;
}
//toString method
public String toString() {
return "Employee: [FirstName = " + firstName + ", LastName = " + lastName + ", SSN = " + SSN + ", BankAccount = "
+ bankAccount + "]";
}
public boolean equals(Object other) {
return (lastName.equals(((Student)other).getLastName()) &&
firstName.equals(((Student)other).getFirstName())&&
SSN.equals(((Student)other).getSSN()) &&
bankAccount.equals(((Student)other).getBankAccount()));
}
//Sorting the array based on SSN
public int compareTo(Student key) {
return SSN.compareTo(key.getSSN());
}
}
我在哪里为 binarySearch 排序我的数组
package binarySearch;
public class ObjectBubbleSorter {
public static void bubbleSort(Comparable[] array) {
int lastPos;
int index;
Comparable temp;
for(lastPos = array.length-1; lastPos >= 0; lastPos -= 1) {
for(index = 0; index <= lastPos - 1; index+=1) {
if(array[index].compareTo(array[index+1]) > 0) {
temp = array[index];
array[index] = array[index+1];
array[index+1] = temp;
}
}
}
}
}
我在哪里执行我的 binarySearch
package binarySearch;
public class ObjectBubbleSortTest {
public static int binarySearch(Student list[], Student key) {
int low = 0;
int high = list.length - 1;
int middle = (low + high + 1)/2;
int location = -1;
while((low <= high) && (location == -1)){
if (list[middle].equals(key)) { //location current middle
location = middle;
}
else if(list[middle].compareTo(key) < 0 ) { //middle too high
high = middle - 1;
}
else {
low = middle + 1;
}
middle = (low + high + 1)/2;
}
return location;
}
public static void main(String[]args) {
Student[] student = new Student[5];
//order: First Name, Last Name, SSN, Bank_Account_Number
student[0] = new Student("Adam", "Sarrone", "1234567", "9022345");
student[1] = new Student("Ryan", "Petrowvoksi", "4345123", "0120345");
student[2] = new Student("Jenn", "Henderson", "8124512", "564214");
student[3] = new Student("Ricky", "Jean", "3512345", "612345");
student[4] = new Student("Dare", "Ogun", "421451", "198213");
System.out.println("Original array order: \n");
for (Student element : student)
System.out.print(element + "\n");
//sorting array
ObjectBubbleSorter.bubbleSort(student);
System.out.println();
System.out.println("\nSorted array order: \n");
for (Student element : student)
System.out.print(element + "\n");
System.out.println();
//creating student obj
Student student1 = new Student("Ryan", "Petrowvoksi", "4345123", "0120345");
int studentSSN = binarySearch(student, student1);
System.out.print(studentSSN);
System.out.print(student1.getFirstName() + " " + student1.getLastName() + " was found at position: " + studentSSN);
}
当我执行该 binarySearch 它总是返回 -1 而不是学生元素位置
解决方案
将此行更改list[middle].compareTo(key) < 0
为while 循环list[middle].compareTo(key) > 0
内部。binarySearch
您的功能似乎compareTo
与您希望的相反。
顺便说一句,我建议您将binarySearch更改为这个更易读的:
public static int binarySearch(Student list[], Student key) {
int low = 0;
int high = list.length - 1;
int middle;
int location = -1;
while (low <= high) {
middle = (low + high + 1) / 2;
int compare = list[middle].compareTo(key);
if (compare == 0) {
location = middle;
return location;
} else if (compare > 0) {
high = middle - 1;
} else {
low = middle + 1;
}
}
return location;
}
推荐阅读
- python - 在python中过滤具有定义阈值的排序数组
- node.js - 如何只允许我的阵列中的电子邮件能够注册一个帐户
- c++ - 为什么 adjustSize 不会在 Qt 中调整 MainWindow 的大小?
- vector - 矢量点积如何与空间中的父对象旋转相关联?
- visual-studio - 发布没有源代码的 NuGet 包
- sql-server - 将列输出为来自不同表的不同数字
- javascript - 如何使用 jquery 优化图像分辨率?
- angular - 我如何更改分页器?
- plantuml - PlantUML - 带有嵌套支架的详细描述
- python - Pandas:将日期写入 Excel,以便它们可用作日期