我目前处于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
什么不你了解了错误信息? –
原始类型对我来说有点模糊。他让我最多的东西是转换错误。 –
您正在传递double数组,并且方法期望为Integer数组。 – RamPrakash