您好,我想一下,折半查找的程序里面,我加了泛型修饰,但是后面我在主函数里面调用这个函数的时候,显示编译错误,说传入的变量不是泛型类型
那么请问这个泛型是为了演示才这么写的还是我后面主函数里写错了东西?谢谢!抱拳
import java.util.*;
public class Demo{
private static<T extends Comparable<T>> int binarySearch(T[] x, int low, int high, T key) {
if(low <= high) {
int mid = low + ((high -low) >> 1);
if(key.compareTo(x[mid])== 0) {
return mid;
}
else if(key.compareTo(x[mid])< 0) {
return binarySearch(x,low, mid - 1, key);
}
else {
return binarySearch(x,mid + 1, high, key);
}
}
return -1;
}
}
public static void main(String[] args) {
int[] b={4,12,25,43,54,76,99,567,676};
System.out.println(binarySearch(b,0,8,54));
}
}