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

あなたは二重配列を渡しており、メソッドは整数配列を期待しています。 – RamPrakash

答えて

0

を失うことになる(私は改善の余地がたくさんあるけど、私はうまく動作するプログラムの変更の最小数を示唆しています)

メソッド

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

は整数を想定していますが、double型の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が期待していますIntegerの配列とパラメータとしてのint line 25の呼び出しでは、double配列とdoubleをパラメータとするので、例外は例外です。binary searchを呼び出しています。

倍精度浮動小数点型はint型よりも情報量が多いため、double型をint型に変換することはできません。 intに変換され、二重43.5は0.5

+0

期待を変えるにはどうすればいいですか? –

+0

メソッド宣言の引数の型を変更するか、それを使用するときに渡される引数を変更します。 あなたはpublic static void binarySearch(Double [] a、double x)のようにすることができます{ – Zeromus

関連する問題