亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么此鏈表遍歷有效?參考文獻如何工作?

為什么此鏈表遍歷有效?參考文獻如何工作?

ibeautiful 2022-09-07 21:12:15
在C中,這個概念在指針中變得非常清晰,但是我很難理解java中到底發生了什么。有人可以向我解釋一下,當我在demoveNode()中遍歷列表時,它不會改變原始對象上的任何東西,但是當我做front.next = front.next.next時,它實際上會改變對象。它讓我瘋狂,因為在C中,我可以使用指針來編輯w / e我想要的。參考文獻到底是怎么回事?注意:我知道此代碼不處理邊緣情況。如空節點等...public class LLnode{    int value;    LLnode next;    public LLnode(int x){        this.value = x;        this.next = NULL;    }}/* * This fn removes the node with the specified value n from the linked list */public void removeNode(LLnode head, int n){    LLnode front = head;    while (front.next.value != n){        front = front.next;  //why DOESN'T this physically change the LL?    }    front.next = front.next.next;  //why DOES this physically change the LL ?}public static void main(String[] args){    //node creation    LLnode a = new LLnode(10);    LLnode b = new LLnode(20);    LLnode c = new LLnode(30);    LLnode d = new LLnode(40);    //assignments    c.next = d;    b.next = c;    a.next = b;    removeNode(a,30);}謝謝。
查看完整描述

1 回答

?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

Java 是按值傳遞的。 將參照值從 復制到 中。因此,對 .在循環中創建只是為了指向當前元素,它不用于維護列表。front = headheadfrontfront = front.nextheadfront

但是,會更改 所引用的對象中的字段。此處沒有字段的引用副本,就像以前是 的副本一樣。front.next = front.next.nextnextfrontnextfronthead


查看完整回答
反對 回復 2022-09-07
  • 1 回答
  • 0 關注
  • 72 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號