Java 分治算法-结果NullPointerException

Java 分治算法-结果NullPointerException,java,divide-and-conquer,Java,Divide And Conquer,我正在做一个使用分而治之方法的程序。不幸的是,我不能使用find(…)方法。 在这一行中,我不知道如何代替null:find(null,01000,34)。 先谢谢你 public class Main { static int find (double T[], int left, int right, double numToSearch) { if (left <= right) { int middle = (left + right

我正在做一个使用分而治之方法的程序。不幸的是,我不能使用
find(…)
方法。 在这一行中,我不知道如何代替null:
find(null,01000,34)
。 先谢谢你

public class Main {
    static int find (double T[], int left, int right, double numToSearch) {
        if (left <= right) {
            int middle = (left + right) / 2;
            if (T[middle] == numToSearch) {
                return middle;
            }
            if (T[middle] < numToSearch) {
                return find(T, middle+1, right, numToSearch);
            }
            return find(T, left, middle-1, numToSearch);
        }
        return -1;      
    }

    public static void main(String[] args) {
        System.out.println(find(null /* (OR WHAT HERE TO MAKE IT WORK) */, 0, 100, 34));
    }
公共类主{
静态整数查找(双T[],左整数,右整数,双整数搜索){

如果(left您必须提供一个数组,
double[]
。此外,您必须调用
find(…)
方法,使用此数组作为第一个参数和有效的第三个参数(`right``不得大于数组的最大索引)

请参阅此代码,它基本上是您的代码加上一个示例数组、对
find
方法的正确调用以及
main
方法中的一些(希望有用)注释:

public class Main {

    static int find(double T[], int left, int right, double numToSearch) {
        if (left <= right) {
            int middle = (left + right) / 2;
            if (T[middle] == numToSearch) {
                return middle;
            }
            if (T[middle] < numToSearch) {
                return find(T, middle + 1, right, numToSearch);
            }
            return find(T, left, middle - 1, numToSearch);
        }
        return -1;
    }

    public static void main(String[] args) {
        // create an array that you can use for searching a number, here it is 34 at index 8
        double[] array = {1, 2, 3, 4, 5, 6, 7, 30, 34, 44, 45, 66, 67, 71, 72, 73, 77, 85, 89, 90, 99};
        // use the find-method with a valid maximum index (right) and the array defined before
        System.out.println(find(array, 0, array.length - 1, 34));
    }

}

你想在其中查找数字的数组?不幸的是,我不能使用“find”方法-你知道这是调用
find
方法->
System.out.println(find(null/*(或这里的什么使它工作)*/,0,100,34));
问问你自己:我想从哪个数组中查找
34
public static void main(String[] args) {
    // create an array that you can use for searching a number, here it is 34 at index 8
    double[] array = {1, 2, 3, 4, 5, 6, 7, 30, 34, 44, 45, 66, 67, 71, 72, 73, 77, 85, 89, 90, 99};
    double numberToFind = 34;
    // use the find-method with a valid maximum index (right) and the array defined before
    System.out.println("The number " + numberToFind + " is at index " + find(array, 0, array.length - 1, 34));
}