ArrayList的克隆问题
ArrayList<String> ls = new ArrayList<String>(); // xx为自定义类 ... ArrayList<String> clone = (ArrayList<String>)ls.clone(); // 此语句报如下Warning
Type Safety: The cast from ArrayList to ArrayList<xx> is actually checking against the erased type ArrayList.
type safety: erased type Type safety: The cast from Object to ArrayList<String> is actually checking against the erased type ArrayList.
The compiler is warning you that the cast you are doing is only ensuring the Object is an ArrayList. It can't tell if it truly is an ArrayList<String>. If it isn't, on your head be it. If it is not really an ArrayList< String> expect a ClassCastException as soon
as you do a get, even though there are no explicit casts near the get in your code. The problem in essense is that serialised objects contain no record of their generic type. Many think that design decision was a big mistake
ArrayList的clone()方法如何不能自动克隆ArrayList包含的每一个对象——原有ArrayList和克隆后的ArrayList是相同对象的别名。
这种情况通常叫做浅拷贝,因为它仅仅复制一个对象的“表面”部分。实际的对象由这个“表面”,引用指向的所有对象,以及那些对象指向的所有对象等构成。这往往被称作“对象网络”。如果你拷贝所有这些内容,则被称为深拷贝。
上一篇: log4j
下一篇: 面试试题001-截取字符串