Java 中的克隆列表
可能的重复:
如何克隆 ArrayList 并克隆其内容?
我有一个 ArrayList
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
什么是实现这一目标的最佳/最安全方法?我想这并不那么简单:
ArrayList<Widget> cloneList = origList.clone();
我想这是一个内置的 ArrayList 类型,再加上它的泛型这一事实,将使事情变得复杂。我还想象我需要为我的 Widget
类编写一个特殊的 clone()
重写?
提前致谢!
编辑:
如果有一个公共 JAR 可以为我完成这项繁重的工作,我也会完全接受,所以请随意提出建议,但是我仍然想知道如何以 ole' 时尚的方式做到这一点,这样我就可以学习 ;-)
Possible Duplicate:
How to clone ArrayList and also clone its contents?
I have a ArrayList<Widget>
that I would like to "deep" clone so that making modifications to any items in the original list does not have any effect on the items in the cloned list:
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
What's the best/safest way of accomplishing this? I imagine that it's not as simple as:
ArrayList<Widget> cloneList = origList.clone();
I imagine the fact that this is a built-in ArrayList
type, plus the fact that its generic, are going to complicate things. I also imagine I will need to write a special clone()
override for my Widget
class?
Thanks in advance!
Edit:
I'd also be completely receptive if there is a commons JAR out there that does this heavy lifting for me, so please feel free to make suggestions, however I would still like to know how to do this the ole' fashion way so I can learn ;-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一项艰巨的任务,我建议您使用可用的库之一,例如 http:// code.google.com/p/cloning/
另请参阅:Java:深度克隆/复制实例的推荐解决方案
如果您想了解它是如何完成的,获取开源库并查看源代码:)
This is a non-trivial task, I suggest you use one of the available libraries such as http://code.google.com/p/cloning/
See also: Java: recommended solution for deep cloning/copying an instance
If you want to see how it's done get an open-source library and look at the source :)
您需要迭代原始列表中的每个项目并单独克隆每个项目,然后将它们添加到“克隆”项目的新列表中。
类似的:
要实现此功能,您必须让
Widget
对象实现Cloneable
接口和clone()
方法。不需要其他任何东西。但正如其他发帖者所说,许多人会认为 Java 中的克隆实现不太值得依赖,最好避免。
You need to iterate over each item in the original list and clone each item individually, then add them to a new list of 'cloned' items.
Something like:
For this to work you will have to have your
Widget
object implement theCloneable
interface, and theclone()
method. Nothing else is needed.But again, as the other posters have said, many would argue that the
clone
implementation in Java isn't great to rely on, and is best avoided.一些当局不鼓励使用
克隆
。 这是一个来自 google 的链接。这并不是说不要这样做,而是要意识到自己正在做什么,并一定要进行测试(好吧,总是这样做)。我可能会在根类上放置一个deepCopy方法,然后使用复制构造函数以强力方式进行复制。复制构造函数是一种构造函数,它采用相关类的实例,并创建一个新实例,将参数的内部状态复制到新实例中。
Some authorities discourage the use of
clone
. Here is one link off google. That's not to say don't do it, but just be aware of what you are getting yourself into, and be sure to test (well, always do that).I would probably put a
deepCopy
method on the root class, and just do the copy the brute force way, with copy constructors. A copy constructor is a constructor that takes an instance of the class in question, and creates a new instance, copying the internal state of the argument into the new instance.您可能想看看这个:http://code.google.com/p/cloning/
You might want to take a look at this: http://code.google.com/p/cloning/