我正在閱讀Hortsmann和Cornell(Vol.1)的書「Core Java」。 在「泛型」一章中,他們提出了一個故意輸入,以便查看編譯器所說的內容。我的問題是:不僅編譯器不會給我同樣的錯誤,而且當我按照本書所述更正輸入時,它也會一直給我提供錯誤。這裏的交易:Java泛型:「核心Java」教程的解決方案不適用於Eclipse?
public class PairTest1 {
public static void main(String[] args) {
double middle = ArrayAlg.getMiddle(3.14, 1729, 0);
}
}
class ArrayAlg {
public static <T> T getMiddle(T[] a) {
return a[a.length/2];
}
}
書上說我應該得到的錯誤:
found: java.lang.Number&java.lang.Comparable>, required: double.
取而代之的是,Eclipse將不會讓我跑程序,並稱
The method getMiddle(T[]) in the type ArrayAlg is not applicable for the arguments (double, int, int)
這本書,然後說
the remedy is to write all parameters as double values.
我爲此e從1729改爲1729.1,從0改爲0.1。現在Eclipse中說:
The method getMiddle(T[]) in the type ArrayAlg is not applicable for the arguments (double, double, double)
Mmmhh ...我沒有得到什麼是怎麼回事。建議?
謝謝&問候
編輯:我試圖創建陣列double[] doubles = { 3.14, 1729.1, 0.1 };
,並給它作爲參數,但現在我得到的錯誤
本書使用的是哪個版本的JDK?你使用什麼版本?這本書是否使用Eclipse? –