【发布时间】:2016-02-01 12:39:48
【问题描述】:
我想弄清楚为什么我的代码会出错 -
线程“main”java.lang.Error 中的异常:未解决的编译问题:
The method maximum(T[]) in the type find_max is not applicable for the arguments (int[])
The method maximum(T[]) in the type find_max is not applicable for the arguments (double[])
代码
public static <T extends Comparable<T>> T maximum(T [] a) throws Exception {
System.out.println("here");
T max=a[0]; // assume x is initially the largest
for (T i : a) {
if (i.compareTo(max) > 0) {
max=i;
}
}
return max; // System.out.println("Max is " + max);
}
【问题讨论】:
-
public static
> T maximum(T [] a) throws Exception { T max=a[0]; // 假设 x 最初是最大的 for (T i : a) { if (i.compareTo(max) > 0) { max=i; } } 返回最大值; } -
能否提供代码sn-p和错误堆栈?
-
因为
int和double是原始类型,不能用于泛型。 -
忘记放在那里了:P