IAm*_*aja 0 java generics clone list
可能重复:
如何克隆ArrayList并克隆其内容?
我有一个ArrayList<Widget>我想"深入"克隆的内容,以便对原始列表中的任何项进行修改对克隆列表中的项目没有任何影响:
ArrayList<Widget> origList = getMyList();
ArrayList<Widget> cloneList = origList.clone();
// Remove the 5th Widget from the origina list
origList.remove(4);
// However, the cloneList still has the 5th Widget and is unchanged
// Change the id of the first widget
origList.get(0).setId(20);
// However in cloneList, the 1st Widget's ID is not 20
Run Code Online (Sandbox Code Playgroud)
什么是实现这一目标的最佳/最安全的方法?我想它并不像以下那么简单:
ArrayList<Widget> cloneList = origList.clone();
Run Code Online (Sandbox Code Playgroud)
我想象这是一个内置ArrayList类型的事实,再加上它的泛型,会使事情变得复杂.我还想象我需要clone()为我的Widget班级写一个特殊的覆盖?
提前致谢!
编辑:
如果有一个公共JAR在那里为我做这个繁重的工作,我也会完全接受,所以请随意提出建议,但我仍然想知道如何做这个时尚方式所以我可以学 ;-)
| 归档时间: |
|
| 查看次数: |
13924 次 |
| 最近记录: |