gpt4 book ai didi

java - 快速排序方法异常

转载 作者:行者123 更新时间:2023-12-02 11:24:48 24 4
gpt4 key购买 nike

我试图将文件中的名称读取到字符串数组中,并使用二分搜索根据用户输入的内容查找名称,但我不断收到空指针异常?当查看是否有任何内容返回 null 时,我觉得它与比较器有关,但我不能 100% 确定是否是这样。

    import java.util.*;
import java.io.*;
public class nameSearch{

static String names[];
int length;

public static void main(String[] args)throws IOException{

nameSearch sorter = new nameSearch();
File f = new File("names.txt");
Scanner scan = new Scanner(f);
Scanner input = new Scanner(System.in);
names = new String[65];

int counter = 0;

while(scan.hasNext()){
counter = counter + 1;
scan.next();
for(int i=0; i < counter; i=i+1){
names[i] = scan.next();
System.out.println(names[i].toString());
}
}

sorter.sort(names);
System.out.println(names.toString());
scan.close();

System.out.println("Enter a name you want to search: ");
String s = input.nextLine();
sorter.binarySearch(s, names);

}

public String toString(){
return "Name: "+ names;
}



void sort(String[] array) {
if (array == null || array.length == 0) {
return;
}
this.names = array;
this.length = array.length;
quickSort(0, length - 1);
}

void quickSort(int lowerIndex, int higherIndex) {
int i = lowerIndex;
int j = higherIndex;
String pivot = this.names[lowerIndex + (higherIndex - lowerIndex) / 2];

while (i <= j) {
while (this.names[i].compareToIgnoreCase(pivot) < 0) {
i++;
}

while (this.names[j].compareToIgnoreCase(pivot) > 0) {
j--;
}

if (i <= j) {
exchangeNames(i, j);
i++;
j--;
}
}

if (lowerIndex < j) {
quickSort(lowerIndex, j);
}
if (i < higherIndex) {
quickSort(i, higherIndex);
}
}

void exchangeNames(int i, int j) {
String temp = this.names[i];
this.names[i] = this.names[j];
this.names[j] = temp;
}

void binarySearch(String s, String[] ar){
String stringToFind = s;
int lowestIndex = 0;
int highestIndex = ar.length-1;
int middleIndex = 0;

while(lowestIndex<=highestIndex){
middleIndex = (lowestIndex+highestIndex)/2;

if(stringToFind.compareTo(ar[middleIndex]) > 0){
lowestIndex = middleIndex+1;
}else if(stringToFind.compareTo(ar[middleIndex]) < 0){
highestIndex = middleIndex - 1;
}else{
break;
}
}

if(lowestIndex > highestIndex){
System.out.println("not found");
}else{
System.out.println("found at " + middleIndex);
}
}



}

编译器告诉我,sorter.sort(names)、quicksort(0, length-1) 上存在空指针异常,并且从以下位置开始:
while (this.names[i].compareToIgnoreCase(pivot) < 0) {

最佳答案

在第 16 行中,您已将 names 定义为 String[65],并且您的文件中可能没有 65 个名称。所以一些 names 元素为空。尝试使用 arrayList。

public class NameSearch{

static ArrayList<String> names;
int length;

public static void main(String[] args)throws IOException{

NameSearch sorter = new NameSearch();
File f = new File("src/names.txt");
Scanner scan = new Scanner(f);
Scanner input = new Scanner(System.in);
names = new ArrayList<>();

int counter = 0;

while(scan.hasNext()){
names.add(scan.next());
}

sorter.sort(names);
System.out.println(names.toString());
scan.close();

System.out.println("Enter a name you want to search: ");
String s = input.nextLine();
sorter.binarySearch(s, names);

}

public String toString(){
return "Name: "+ names;
}



void sort(ArrayList<String> array) {
if (array == null || array.size() == 0) {
return;
}
this.names = array;
this.length = array.size();
quickSort(0, length - 1);
}

void quickSort(int lowerIndex, int higherIndex) {
int i = lowerIndex;
int j = higherIndex;
String pivot = this.names.get(lowerIndex + (higherIndex - lowerIndex) / 2);

while (i <= j) {
while (this.names.get(i).compareToIgnoreCase(pivot) < 0) {
i++;
}

while (this.names.get(j).compareToIgnoreCase(pivot) > 0) {
j--;
}

if (i <= j) {
exchangeNames(i, j);
i++;
j--;
}
}

if (lowerIndex < j) {
quickSort(lowerIndex, j);
}
if (i < higherIndex) {
quickSort(i, higherIndex);
}
}

void exchangeNames(int i, int j) {
Collections.swap(this.names, i, j);
}

void binarySearch(String s, ArrayList<String> ar){
String stringToFind = s;
int lowestIndex = 0;
int highestIndex = ar.size()-1;
int middleIndex = 0;

while(lowestIndex<=highestIndex){
middleIndex = (lowestIndex+highestIndex)/2;

if(stringToFind.compareTo(ar.get(middleIndex)) > 0){
lowestIndex = middleIndex+1;
}else if(stringToFind.compareTo(ar.get(middleIndex)) < 0){
highestIndex = middleIndex - 1;
}else{
break;
}
}

if(lowestIndex > highestIndex){
System.out.println("not found");
}else{
System.out.println("found at " + middleIndex);
}
}
}

关于java - 快速排序方法异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49700155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com