Java 中的克隆列表

发布于 2024-12-27 11:53:31 字数 1038 浏览 2 评论 0原文

可能的重复:
如何克隆 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你不是我要的菜∠ 2025-01-03 11:53:31

这是一项艰巨的任务,我建议您使用可用的库之一,例如 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 :)

于我来说 2025-01-03 11:53:31

您需要迭代原始列表中的每个项目并单独克隆每个项目,然后将它们添加到“克隆”项目的新列表中。

类似的:

List<Widget> origList = getMyList();
List<Widget> clonedList = clone(origList);

private List<Widget> clone(List<Widget> listToClone) {
  List<Widget> clonedList = new LinkedList<Widget>();

  for (Widget widget : listToClone) {
    clonedList.add(widget.clone());
  }

  return clonedList;
}

要实现此功能,您必须让 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:

List<Widget> origList = getMyList();
List<Widget> clonedList = clone(origList);

private List<Widget> clone(List<Widget> listToClone) {
  List<Widget> clonedList = new LinkedList<Widget>();

  for (Widget widget : listToClone) {
    clonedList.add(widget.clone());
  }

  return clonedList;
}

For this to work you will have to have your Widget object implement the Cloneable interface, and the clone() 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.

痴梦一场 2025-01-03 11:53:31

一些当局不鼓励使用克隆这是一个来自 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.

壹場煙雨 2025-01-03 11:53:31

您可能想看看这个:http://code.google.com/p/cloning/

You might want to take a look at this: http://code.google.com/p/cloning/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文