도와주세요. 이것은 숙제 임무이며, 나는 그것이 작동하지 않는 이유를 알 수 없다. 선형 검색 메서드를 호출 할 때 올바른 데이터 형식 인 T를 전달하지 않는다고 말합니다. T는 일반 데이터 형식이어야하므로 왜 힘든 시간을 보내고 있는지 이해할 수 없습니다. 도와주세요!제네릭 데이터 유형을 사용하는 것이 좋습니다
public static void main(String[] args) {
int[] five= new int[5];
int[] ten= new int[10];
int[] fifteen= new int[15];
fillArray(five);
fillArray(ten);
fillArray(fifteen);
int result1 = linearSearch(five, 37);
int result2 = linearSearch(ten, 37);
int result3 = linearSearch(fifteen, 37);
System.out.println(Arrays.toString(five) + " " + result1);
System.out.println(Arrays.toString(ten) + " " + result2);
System.out.println(Arrays.toString(fifteen) + " " + result3);
}
//이 메서드는 채워진 배열에 대해 선형 검색을 수행하며 모든 데이터 형식을 받아 들여야합니다.
public static <T extends Comparable<T>> int linearSearch(T[] array, T data){
int value = -1;
for(int i=0; i<array.length; i++){
if (array[i]== data){
value = i;
}
}
return value;
}
원시는 원시 유형에서 작동하지 않습니다. –
'int [] '대신'Integer []'를 사용해보십시오 – Bohemian