【发布时间】:2016-03-23 15:07:21
【问题描述】:
class GenMethDemo {
static <T, V extends T> boolean isIn(T x, V[] y) {
for (int i = 0; i < y.length; i++)
if (x.equals(y[i]))
return true;
return false;
}
/*when compiled in java 7 it producing an error and compiling in java 8 without error */
public static void main(String args[]) {
Integer nums[] = {1, 2, 3, 4, 5};
String s[] = {"one", "two", "three"};
System.out.println(isIn("fs", nums));
/*
when compiled in java 7 it producing an error and compiling in java 8 without error */
}
}
【问题讨论】:
-
请编辑您的问题以显示描述问题的文本。特别是,您在 Java 7 中遇到了什么错误?
-
isIn("fs", nums)不应该工作,因为在这种情况下,T将是String和V将是Integer,它不会扩展String。但是,Java 8 类型推断可能更宽松,因为它会尝试找到匹配的T = Object和V = Object。
标签: java