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是相同对象的别名。

这种情况通常叫做浅拷贝,因为它仅仅复制一个对象的“表面”部分。实际的对象由这个“表面”,引用指向的所有对象,以及那些对象指向的所有对象等构成。这往往被称作“对象网络”。如果你拷贝所有这些内容,则被称为深拷贝。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值