2017-02-13 33 views
-1

我目前处于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 
+3

什么不你了解了错误信息? –

+0

原始类型对我来说有点模糊。他让我最多的东西是转换错误。 –

+0

您正在传递double数组,并且方法期望为Integer数组。 – RamPrakash

回答

0

(我知道有很多需要改进的地方,但我只是建议修改的最小数量的计划工作)

的方法

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

期待整数,但我们希望它使用双打insted。这意味着,参数应该是double数组,双地发现:

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

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

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

现在,终于,我们必须回到我们一直在寻找通过增加在方法的末尾下面的数量(过了一会儿):

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 
0

从你的方法public static void binarySearch(Integer[] a, int x) {binarySearch声明期待一个整型数组和一个int型数组作为参数, 您在line 25中的调用调用binary search时带有双数组和双精度型参数,因此是例外。

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

+0

我将如何去改变期望? –

+0

您可以更改方法声明中的参数类型,也可以更改使用它时传递的参数。 你可以像public static void binarySearch(Double [] a,double x){ – Zeromus