请问有人能解释一下为什么这段代码无法编译吗?
即使它使用了一个没有提供具体类型T的泛型类,它应该能够在编译时识别出ArrayList保存的是字符串。
public class Test {
public static void main(String[] args){
Container container = new Container();
container.strings.add("test");
String s1 = container.strings.get(0); // does not compile
ArrayList<String> local = container.strings;
String s2 = local.get(0); // does compile
}
static class Container <T>{
ArrayList<String> strings = new ArrayList<String>();
}
}