我很困惑为什么我的泛型方法不接受非泛型参数。我使用compareTo编写了一个泛型排序,并试图通过向其中传递一个int数组来测试它,但是我不断收到错误,eclipse告诉我创建一个接受int数组的方法。一直在网上挖掘但我无法看到我做错了什么线索?
主:
public static int[] arrayTen = new int[10];
public static int[] arrayHundred = new int[100];
public static int[] arrayThousand = new int[1000];
public static int[] arrayMillion = new int[1000000];
public static void main (String[] args){
Sorts selection = new Sorts();
//generate four arrays to test speeds
arrayGenerator();
Sorts.selectionSort(arrayTen);
}
泛型排序:
/**
* Selection Sort
* @param array[] a
*/
public static < E extends Comparable<E>> void selectionSort (E[] a){
for (int i = 0 ; i < (a.length - 1) ; i++){
int smallestNum = i; //index of the smallest number/character/element
for (int x = i + 1; x < a.length; x++){
if (a[x].compareTo(a[smallestNum])<=0){ //compare two elements
smallestNum = x;
}
}
sort(a, i, smallestNum);
}
}
/**
* Swaps the smaller & larger numbers
* @param array[] a
* @param int b
* @param int c
*/
private static <E> void sort ( E[] a, int b, int c ){
if ( b != c){
E temp = a[b];
a[b] = a[c];
a[c] = temp;
}
}