【发布时间】:2012-11-05 10:15:34
【问题描述】:
在以下示例中:
public static void main(String[] args) {
List<String> b = new ArrayList<String>();
first(b);
second(b);
List<List<String>> a = new ArrayList<List<String>>();
third(a);
fourth(a); // doesnt work
}
private static <T> void first(List<T> a){
System.out.println("List of T");
}
private static void second(List<?> a){
System.out.println("List of anything ");
}
private static <T> void third(List<List<T>> a){
System.out.println("List of a List of T ");
}
private static void fourth(List<List<?>> a){
System.out.println("List of a List of anything ");
}
为什么调用 second(b) 有效,但调用 Fourth(a) 却无效?
我收到以下错误:
The method fourth(List<List<?>>) in the type `TestTest` is not applicable for the arguments (`List<List<String>>`)
【问题讨论】:
-
你面对的是
Type Erasure mechanismdocs.oracle.com/javase/tutorial/java/generics/erasure.html -
当然可以,但为什么它不适用于 List
- > ?
-
它不是嵌套的——它是暂时的。差别很大。
标签: java generics java-6 nested-generics