gpt4 book ai didi

Java 通过引用传递与 listNode 问题

转载 作者:行者123 更新时间:2023-12-01 05:36:09 27 4
gpt4 key购买 nike

我有一个类:

class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}

打印链表的函数是:

public static void printLinkedNode(ListNode l){
while(l != null){
System.out.print(l.val+" ");
l = l.next;
}
System.out.println(" ");
}

在我的主函数中,我创建了一个名为 test 的 ListNode:

ListNode test = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
head.next.next.next = new ListNode(4);

如果我做 A:

ListNode fast = head, slow = head;
fast = fast.next.next;
printLinkedNode(head); // I get 1->2->3->4

如果我做 B:

ListNode fast = head, slow = head;
fast.next = fast.next.next;
printLinkedNode(head); // I get 1->3->4

我很困惑,为什么在 A 中,头部是 1->2->3->4,而不是 3->4?我读过一些关于 Is Java “pass-by-reference” or “pass-by-value”? 的帖子, 但还是想不通..

如果我做 C:

            ListNode fast = head, slow = head;
fast = fast.next.next;
printLinkedNode(fast); //3->4
printLinkedNode(head); //1->2->3->4
fast.next = new ListNode(5);
printLinkedNode(fast); //3->5
printLinkedNode(head); //1->2->3->5, why the head will change?

我很困惑为什么当我们执行 fast.next = new ListNode(5) 时头部会改变;我觉得 fast 不是用 head 赋值?

最佳答案

当您赋值一个变量时,您使它指向(引用)一个特定的对象。当你重新分配它时,你让它指向(引用)另一个对象,你不会覆盖它持有的引用值:

ListNode fast = head, slow = head; // fast has a reference to head.
fast = fast.next.next; // fast has a reference to fast.next.next. You are not overwriting head, just fast.
printLinkedNode(head); // I get 1->2->3->4

相反,如果您编辑引用对象内部的内容,您将编辑原始对象:

ListNode fast = head, slow = head; // fast has a reference to head
fast.next = fast.next.next; // by editing fast.next, you edit head.next
printLinkedNode(head); // I get 1->3->4

用例 C 的更新:

ListNode fast = head, slow = head; // fast = head = (1)
fast = fast.next.next; // fast = fast.next.next = (3). head is still (1)
printLinkedNode(fast); // 3->4 -> because fast points to head.next.next (3)
printLinkedNode(head); // 1->2->3->4 -> head wasn't modified by any of the previous instructions

// here fast points to head.next.next. So fast.next is the same as head.next.next.next (4).
fast.next = new ListNode(5); // Overwrites fast.next, it was (4), becomes (5)
printLinkedNode(fast); // 3->5
printLinkedNode(head); // 1->2->3->5

为了更容易理解:

假设我们有 ListNode 类型的对象 abc:

ListNode a = new ListNode(1);
ListNode b = new ListNode(2);
ListNode c = new ListNode(3);

ListNode d = a; // variable d now points to object a
d.next = b; // since d points to a, this statement modifies a.next
d = c // This does *not* modify a. It just makes d point to c.

关于Java 通过引用传递与 listNode 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46058486/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com