为什么我的链接列表在克隆后似乎指向相同的东西?

发布于 2024-12-06 12:12:41 字数 791 浏览 1 评论 0原文

我注意到,当我第一次拥有时

list2 = (LinkedList)list.clone();

,我可以独立地操作两个列表,例如。 list2.remove(1)

但后来当我

list = (LinkedList)list2.clone();

这样做时 list.remove(1) list2 也会受到影响。这是为什么?

更新

我的代码http://pastie.org/2598096

示例输入:

4 8
1 5 2 3
4
I 1 2
R 2
C 1 10
I 4 2

> javac Main.java && java Main < ./input/betterlist0.in 
[ 1, 5, 2, 3, ] -- [ 2, 1, 5, 2, 3, ] // list2 can be modified from list1 independently
YES9 8
[ 2, 5, 2, 3, ] -- [ 2, 5, 2, 3, ] // now when list2 is modified, list1 gets modified too. 

我认为它因为 super.clone() 会进行浅拷贝。但为什么第一次就成功了呢?

I noticed that when I first have

list2 = (LinkedList)list.clone();

I could operate on both lists independently eg. list2.remove(1)

But later when I do

list = (LinkedList)list2.clone();

when I do list.remove(1) list2 is affected too. Why is that?

UPDATE

My code http://pastie.org/2598096

Example input:

4 8
1 5 2 3
4
I 1 2
R 2
C 1 10
I 4 2

> javac Main.java && java Main < ./input/betterlist0.in 
[ 1, 5, 2, 3, ] -- [ 2, 1, 5, 2, 3, ] // list2 can be modified from list1 independently
YES9 8
[ 2, 5, 2, 3, ] -- [ 2, 5, 2, 3, ] // now when list2 is modified, list1 gets modified too. 

I think its because super.clone() makes a shallow copy. But why then did it work the 1st time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

街道布景 2024-12-13 12:12:41

一般来说,您应该编写自己的clone()函数来实现您想要的深复制。因为java不保证这一点。

以下是维基百科的引用:

Object.clone() 的默认实现执行浅复制。当一个类需要深层复制或某些其他自定义行为时,它们必须在从超类获取副本后在自己的clone()方法中执行该操作。

我认为这个< /a> 也值得一读。

In general you should write your own clone() function to achieve the deep copy you want. because java is not guaranteeing this.

Here is a quote from wikipedia:

The default implementation of Object.clone() performs a shallow copy. When a class desires a deep copy or some other custom behavior, they must perform that in their own clone() method after they obtain the copy from the superclass.

And I think this is also worth reading.

千里故人稀 2024-12-13 12:12:41
LinkedList l1 = new LinkedList();
l1.add("A");l1.add("B");

LinkedList l2 = (LinkedList)l1.clone();
out("l2 after clone: "+l2.size());
l2.remove(0);
out("l2 after remove: "+l2.size());
l1 = (LinkedList)l2.clone();
out("l1 cloned from l2: "+l1.size());
l1.remove(0);
out("l1 after remove :"+l1.size());
out("l2 after l1's remove:"+l2.size());

这使得:

l2 after clone: 2
l2 after remove: 1
l1 cloned from l2: 1
l1 after remove :0
l2 after l1's remove:1

这表明

.clone

正在按预期工作。

LinkedList l1 = new LinkedList();
l1.add("A");l1.add("B");

LinkedList l2 = (LinkedList)l1.clone();
out("l2 after clone: "+l2.size());
l2.remove(0);
out("l2 after remove: "+l2.size());
l1 = (LinkedList)l2.clone();
out("l1 cloned from l2: "+l1.size());
l1.remove(0);
out("l1 after remove :"+l1.size());
out("l2 after l1's remove:"+l2.size());

that makes:

l2 after clone: 2
l2 after remove: 1
l1 cloned from l2: 1
l1 after remove :0
l2 after l1's remove:1

that demonstrates

.clone

is working as expected.

淑女气质 2024-12-13 12:12:41

当我执行 list.remove(1) 时,list2 也会受到影响

不,不是。你的观察在这里是错误的。

when I do list.remove(1) list2 is affected too

No it isn't. Your observations are at fault here.

梦境 2024-12-13 12:12:41

这也是可能发生的情况。

基本上,列表可能不保存实际的对象数据,而是对它们的引用。换句话说,它跟踪这些对象的指针(也称为内存地址)。

在英语中,这就像说“这些房子位于这些地址”,并且您将地址列表存储在电话簿中。 clone() 可能不会复制房子,但它可能会复制电话簿

还有可能有人存储“电话簿存储在 B 架上”,将其写下来,然后递给其他人一张纸,上面写着“电话簿存放在 B 架上”

Here's also what might be happening.

Basically, the list is probably not holding the actual object data, but references to them. In other words, it's keeping track of the pointers (a.k.a. memory addresses) to those objects.

In English, this is like saying "These houses are at these addresses", and you are storing the list of addresses in a phone book. The clone() might not be copying the house, but it might be copying the phone book

There's also the chance that someone might be storing "The phone book is stored on shelf B", writing it down, and handing someone else a paper saying that "The phone book is stored on shelf B"

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