2017-02-13 34 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