我必須「實現線性搜索的以下通用方法」,但據我所知,不可能有一個通用數組。我的同班同學中沒有人能夠弄清楚,我們班也沒有TA。這是否可能,如果是的話,你能指出我的方向是正確的嗎?由於通用線性搜索
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String type = scan.next();
int length = scan.nextInt();
//checks the type and makes the appropriate array
if (type.equals("I")) {
int[] anArray = new int[length];
for (int i = 0; i<length; i++) {
anArray[i] = scan.nextInt();
}
int key = scan.nextInt();
linearSearch(anArray, key); //Error, the method is not applicable for the arguments
}
}
public static <E extends Comparable<E>> int linearSearch(E[] list, E key) {
for (int i = 0; i<list.length; i++) {
if (list[i].equals(key)) {
return i;
}
}
return 0;
}