Java 编译器似乎并不关心列表中的类型是否匹配,直到需要获取对象为止:
private static void print(List<Integer> list) { //private static <T> void print(List<T> list) { will also do
for (Object object: list)
System.out.print(object);
int int0 = list.get(0); //ok
//int int1 = list.get(1); //ClassCastException if run
}
public static void main(String[] args) {
List list = new ArrayList();
list.add(1);
list.add("2");
print(list);
}
沧海一幻觉
收到一只叮咚
相关分类