当我搜索元素时,错误出现在空指针处。它成功接受输入并显示数组,但是当我输入搜索元素并按Enter键时,它会显示错误。
它显示空指针异常,带有以下堆栈跟踪。
搜索元素时的错误如下。
Exception in thread "main" java.lang.NullPointerException
at BinarySearchClass.binarySearch(BinarySearchClass.java:26)
at BinarySearchIterative.main(BinarySearchIterative.java:25)
即;它在这一行中说
BinarySearchClass.java:26
while ((array[indexToLook] != search) && (maxIndex > minIndex)) {
和另一个文件中的这一行
BinarySearchIterative.java:25
int found = BSI.binarySearch(search);
请帮我纠正。
import java.util.Arrays;
import java.util.Scanner;
public class BinarySearchClass {
private Integer[] array;
public BinarySearchClass(int size){
this.array = new Integer[size];
}
public BinarySearchClass(){
this(100); //default size of array.
}
public int binarySearch(int search){
int maxIndex = size()-1;
int minIndex = 0;
int indexToLook = (int) Math.floor((minIndex + maxIndex)/2);
while ((array[indexToLook] != search) && (maxIndex > minIndex)) {
if(array[indexToLook] > search) {
maxIndex = indexToLook - 1;
}
else{
minIndex = indexToLook + 1;
}
indexToLook = (int) Math.floor((minIndex+maxIndex/2));
}
if(array[indexToLook] == search)
return indexToLook;
return -1;
}
public int binarySearch( int search, int minIndex, int maxIndex){
if (minIndex == maxIndex){
if(array[minIndex] == search)
return minIndex;
return -1;
}
int indexToLook = (int) Math.floor((minIndex + maxIndex)/2);
if(array[indexToLook] == search){
return indexToLook;
}
if(array[indexToLook] < search)
return binarySearch( search, indexToLook+1, maxIndex );
return binarySearch( search, minIndex, indexToLook-1);
}
public String toString() {
return Arrays.deepToString(this.array);
}
private int size() {
int i = 0;
while (((array[i] != null) && i < array.length) ) {
i++;
}
return i;
}
}
class BinarySearchIterative:
import java.util.Scanner;
public class BinarySearchIterative {
public static void main(String[] args){
BinarySearchClass BSI = new BinarySearchClass(10);
Scanner scan = new Scanner(System.in);
System.out.println("Iterative");
System.out.println("Enter the number of elements: ");
int size = scan.nextInt();
int[] array = new int[size];
int i;
for(i =0; i<=size-1; i++){
System.out.println("Enter the element " + i + " :");
array[i] = scan.nextInt();
}
System.out.println("The elements are");
for(i=0; i<=size-1; i++){
System.out.print("[" + array[i] + "]");
}
int search = 0;
System.out.println("\nEnter the element to search");
search = scan.nextInt();
int found = BSI.binarySearch(search);
if (found > -1)
{
System.out.println("The element " + search + " is found in location " + (found+1) );
}
else
{
System.out.println("Element " + search + " not found");
}
}
}
编辑(@Paul):
由于您要在BinarySearchIterative
中提供要执行二进制搜索的数组,我建议使用此方法而不是您的方法:
import java.util.Arrays;
import java.util.Scanner;
public class BinarySearchClass {
public static int binarySearch(int search , int[] array){
int maxIndex = size()-1;
int minIndex = 0;
int indexToLook = (int) Math.floor((minIndex + maxIndex)/2);
while ((array[indexToLook] != search) && (maxIndex > minIndex)) {
if(array[indexToLook] > search) {
maxIndex = indexToLook - 1;
}
else{
minIndex = indexToLook + 1;
}
indexToLook = (int) Math.floor((minIndex+maxIndex/2));
}
if(array[indexToLook] == search)
return indexToLook;
return -1;
}
public static int binarySearch( int search, int minIndex, int maxIndex , int[] arr){
if (minIndex == maxIndex){
if(array[minIndex] == search)
return minIndex;
return -1;
}
int indexToLook = (int) Math.floor((minIndex + maxIndex)/2);
if(array[indexToLook] == search){
return indexToLook;
}
if(array[indexToLook] < search)
return binarySearch( search, indexToLook+1, maxIndex );
return binarySearch( search, minIndex, indexToLook-1);
}
}
class BinarySearchIterative:
import java.util.Scanner;
public class BinarySearchIterative {
public static void main(String[] args){
...
int found = BinarySearchClass.binarySearch(search , array);
...
}
}
请注意,输入数组必须排序才能执行二进制搜索,而代码无法确保这一点。您的代码存在更一般的设计问题。 BinarySearchClass
不应存储任何数组本身,只提供实用程序方法来搜索数组(使用static
方法,如我的例子)。这将是编程的一般方法:模块化。每个类应仅为特定目的提供功能,或者应仅保存表示单个数据集的数据,并提供处理此数据的功能。看看一般的编码风格指南(关于这个主题有大量的书籍,网络材料等)。