原因是每个数组在运行时都知道其元素类型,而泛型集合不知道类型擦除。
例如:String[] strings = new String[2];Object[] objects = strings; // valid, String[] is Object[]objects[0] = 12;
// error, would cause java.lang.ArrayStoreException: java.lang.Integer during runtime
如果泛型集合允许这样做:List strings = new ArrayList();List objects = strings; // let's say it is validobjects.add(12);
// invalid, Integer should not be put into List but there is no information during runtime to catch this
但是,当有人试图访问列表时,这会引起问题:String first = strings.get(0); // would cause ClassCastException, trying to assign 12 to String