具有原始类型的二进制搜索

时间:2017-02-13 16:50:51

标签: java binary-search primitive

我目前正在Java编程课程中,并且对Java完全不熟悉。我正在尝试创建一个程序,将使用二进制搜索值45.3

class findValue {
public static void main(String args[]) {
    double a[] = new double[6]; //declaration

    a[0] = -3; //initialization
    a[1] = 10;
    a[2] = 5;
    a[3] = 24;
    a[4] = 45.3;
    a[5] = 10.5;

    int n = a.length; //storing length of array
    int temp = 0; //declaring temporary storage place

    for (int i = 0; i < n; i++) {
        for (int j = 1; j < (n - i); j++) {

            if (a[j - 1] > a[j]) {
                temp = (int)a[j - 1];
                a[j - 1] = a[j];
                a[j] = temp; //bubble sorting
            };
        };
    };
    System.out.println("45.3 found" + binarySearch(a, 45.3));
};
public static void binarySearch(Integer[] a, int x) {
    int low = 0;
    int high = a.length - 1;
    int mid; //values for binary search

    while (low <= high) {
        mid = (low + high) / 2; //setting value for searching

        if (a[mid].compareTo(x) < 0) {
            low = mid + 1;
        }
        else if (a[mid].compareTo(x) > 0) {
            high = mid - 1;
        };
    };
};

这是我得到的编译器错误:

Line: 25
method binarySearch in class findValue cannot be applied to given types;
required: java.lang.Integer[],int
found: double[],double
reason: actual argument double[] cannot be converted to java.lang.Integer[] by method invocation conversion

2 个答案:

答案 0 :(得分:0)

从您的方法声明public static void binarySearch(Integer[] a, int x) { binarySearch期望一个Integer数组和一个int作为参数, 虽然您在line 25中的调用是使用double数组和double参数调用binary search,因此异常。

你不能将double转换为int,因为double有比“int”更多的“信息”。双43.5转换为int将失去.5

答案 1 :(得分:0)

(我知道还有很大的改进空间,但我只是建议程序的最小更改次数)

方法

public static void binarySearch(Integer[] a, int x) {...}

期待整数,但我们希望它使用双打。这意味着参数应该是一个双精度数组,而双精度数应该是:

public static void binarySearch(double[] a, double x) {...}

这就是说,我们知道这个函数会返回一个int,所以我们设置了返回类型:

public static double binarySearch(double[] a, double x) {...}

现在,最后,我们必须通过在方法结束时添加以下内容(在while之后)返回我们要查找的数字:

return mid;

最终结果应为:

class findValue {
    public static void main(String args[]) {
        double a[] = new double[6]; //declaration

        a[0] = -3; //initialization
        a[1] = 10;
        a[2] = 5;
        a[3] = 24;
        a[4] = 45.3;
        a[5] = 10.5;

        int n = a.length; //storing length of array
        int temp = 0; //declaring temporary storage place

        for (int i = 0; i < n; i++) {
            for (int j = 1; j < (n - i); j++) {

                if (a[j - 1] > a[j]) {
                    temp = (int)a[j - 1];
                    a[j - 1] = a[j];
                    a[j] = temp; //bubble sorting
                }
            }
        }
        System.out.println("45.3 found: " + binarySearch(a, 45.3));
    }
    public static int binarySearch(double[] a, double x) {
        int low = 0;
        int high = a.length - 1;
        int mid = (low + high) / 2; //values for binary search

        while (low <= high) {
            mid = (low + high) / 2; //setting value for searching

            if (Double.compare(a[mid], (double)x) < 0) {
                low = mid + 1;
            }
            else if (Double.compare(a[mid], (double)x) > 0) {
                high = mid - 1;
            }
        }
        return mid;
    }
}

输出:

45.3 found: 5