[英]Pass by reference for nodes in java linked list
private Node<T> copyList(Node<T> cn1) {
Node<T> temp = cn1;
Node<T> newHead = null;
Node<T> end = null;
newHead = new Node<T>((T)(temp.x).clone(), null);
end = newHead;
temp = temp.next;
while (temp != null) {
end.next = new Node<T>((T)(temp.x).clone(), null);
end = end.next;
temp = temp.next;
}
// Now the entire list is created, just return its head pointer
return newHead;
}
The method is trying to make deep copies for generic nodes in a LinkedList
, the method is correct and I'm just confused about the line end = newHead;
该方法试图为
LinkedList
中的通用节点制作深层副本,该方法是正确的,我只是对end = newHead;
行感到困惑. . Shouldn't
end
and newHead
reference the same object? end
和newHead
不应该引用相同的 object 吗? Why is the line end = end.next;
为什么行
end = end.next;
not updating newHead
?不更新
newHead
?
I'm sorry if the question is too obvious.如果问题太明显,我很抱歉。
When you write something like:当你写这样的东西时:
Node x = new Node();
Where Node is NOT a primitive (so not int, long, boolean, etc),其中 Node不是原语(所以不是 int、long、boolean 等),
you should mentally model that as:你应该在精神上 model 认为:
We create some new treasure ( new Node();
) - we conjure up a treasure box out of thin air.我们创造了一些新的宝藏(
new Node();
)——我们凭空变出一个宝箱。 It has no name;它没有名字; treasures never have names.
宝物从来没有名字。 You bury it in the sand.
你把它埋在沙子里。
Separately, this also declares that you have a treasure map on you, which you've labelled x
(maps have names; treasures do not).另外,这也声明你身上有一个宝藏 map,你已将其标记为
x
(地图有名称;宝藏没有)。 You draw a map to the site you just buried your newly created treasure on this map.你画一个 map 到你刚刚在这个 map 上埋葬你新创造的宝藏的地方。
Then, the .
然后,
.
operator is the 'follow the map and dig' operator, whereas eg the =
operator is the 'wipe out your map and overwrite it with how to get to some other treasure'.运算符是“跟随 map 并挖掘”运算符,而例如
=
运算符是“清除您的 map 并用如何到达其他宝藏的方式覆盖它”。
With 'treasure' = object, and 'map' = variables and expressions. 'treasure' = object,'map' = 变量和表达式。
For example, List<List<String>>
is not really a 'list of a list of strings'.例如,
List<List<String>>
并不是真正的“字符串列表列表”。 It's a list (treasure) that contains treasure maps , not a treasure holding treasures.这是一个包含藏宝图的列表(宝藏),而不是藏宝。
For your specific question:对于您的具体问题:
end = newHead;
is wiping out whatever the treasure map named end
had (it does NOT follow the end
map and do anything to the treasure you find there!) - and overwrites the map with the exact same map that newHead
is. is wiping out whatever the treasure map named
end
had (it does NOT follow the end
map and do anything to the treasure you find there!) - and overwrites the map with the exact same map that newHead
is. Now if you were to follow the newHead
map, you get to the same treasure as if you followed the end
map.现在,如果您跟随新
newHead
,您将获得与跟随end
map 相同的宝藏。 To follow maps, you use the .
要跟随地图,请使用
.
(or, technically, array access, synchronized, throw, and foreach, which also follow maps and operate on the treasure it leads to). (或者,从技术上讲,数组访问、同步、抛出和 foreach,它们也遵循地图并在它通向的宝藏上进行操作)。
So, you have 2 treasure maps, that currently lead to the same treasure.所以,你有 2 张藏宝图,目前通向同一个宝藏。
Then you do: end = end.next;
然后你做:
end = end.next;
. . This does NOT follow the map to the treasure.
这不跟随 map 到宝。 This modifies the treasure map itself: end is wiped out and now is a map to the treasure that the
end.next
map leads to.这修改了宝藏 map 本身: end 被消灭,现在是 map 到
end.next
map 导致的宝藏。
end.next
is like this: "Take the map named end
. Follow the map, and dig down. Open the treasure chest. Inside, you will find another treasure map by looking in the little box labelled 'next' inside the treasure chest. In this code ( end = end.next;
) that treasure map is never followed, it's just copied onto the end
map you've got on you, which wipes out what end
was pointing at before. end.next
是这样的:“取名为end
的 map 。跟着 map 往下挖。打开宝箱。在里面,你会发现另一个宝物 map 在里面的小盒子里找到。在这段代码( end = end.next;
)中,宝藏 map 从未被遵循,它只是被复制到你身上的 map 的end
,这消除了end
之前指向的内容。
This is the situation after end = newHead;
这是
end = newHead;
之后的情况: :
newHead
and end
are 2 different references that point at the same object (the head of the list). newHead
和end
是 2 个不同的引用,它们指向同一个 object(列表的头部)。
This is the situation after end = end.next;
这是
end = end.next;
: :
end
is now pointing to the next node. end
现在指向下一个节点。 newHead
is unaffected since it is a different reference so it keeps pointing what it was pointing before. newHead
不受影响,因为它是一个不同的参考,所以它一直指向它之前指向的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.