0
雖然學習Java的泛型參數化,我想出了這個代碼:調用參數化方法
public interface Comparable<T> {
public int compareTo(T o);
}
public static <T extends Comparable<T>> int countGreaterThan(T[] anArray, T elem) {
int count = 0;
for (T e : anArray)
if (e.compareTo(elem) > 0)
++count;
return count;
}
我想測試它,所以我寫了這個:
Integer[] iArray = new Integer[10];
for (int i=0; i<10; i++){
iArray[i] = new Integer(i);
}
int a = countGreaterThan(iArray, Integer.valueOf(5));
但是編譯器是給我的錯誤調用方法countGreaterThan
當最後一行信息:
The method countGreaterThan(T[], T) in the type Main is not applicable for the arguments (Integer[], Integer)
我失去了一些東西明顯?
噢,真的... 這是愚蠢的例子,我發現... 謝謝! – ajuric