编辑:包含答案的更新信息
我试图有一个全面的观点,收集不同的方式复制。我想我得到了这个概念,但我需要如此四舍五入,我也需要有这样一个总结(请随时修改我的帖子)是有用的。我有:
集合a,b;//I省略泛型
那么我复制收藏的机会有多大呢?:
所以我想这些案例:
收集复制/复制(或返回DAO数据)
SyncStateOfListing OriginalListingSafe OriginalObjectsStateSafe
Mutual aware of changs
**********************
Assignment in other var y - Mirrored n n
Iterator* y - Concurrnt.Exc n (remove()) n
New is aware of old´ changes (RO ProxyView)
******************************************
Coll.unmodifiableColl(orig) y - Mirrored** y NotSuppOpExc on RW n
Coll.unmodif(orig).iterator()* y - Concurrnt.Exc y NotSuppOpExc on RW n
Enum y - ? y No RW methods
Independent copies
******************
Shallow Copy n y n
Deep Copy n y y (expensive)
* Iterator we force to regenerate it in any use and we get Exceptions if at some moment the snapshot is obsolete, so we assure the state for which the operation is meant, transactionally. Cannot add elements or retraverse.
** The "new" collection is totally mirrored for any change on the original, but internally, inner iterator is always used when traversing (the obvious stateful operation) so we get the same properties as when using it我的假设还好吗?
发布于 2012-07-24 13:41:25
作业:a和b将指向相同的列表,因此那里不会有任何副本。
浅薄和懒散在我看来是等同的。对列表中对象的引用将是相同的。您可以这样说:“两个列表中将出现相同的对象”。因此,修改一个列表中的一个对象也会在第二个列表中修改它,因为这是同一个对象。
最后,我认为你应该把这篇文章写得很深:
b = new ArrayList();
for (Object o : a) b.add(o.clone()); // and not a.clone()这样,您就可以修改列表a中的一个对象,而不必修改列表b中的副本。
警告:使用克隆对象有一些限制。有关详细信息,请参阅JDK文档。
https://stackoverflow.com/questions/11631660
复制相似问题