无法理解 Objects 并在 Java 的链表中修改它们的变量

Trouble Understanding Objects and Modifying Variables of them in Linked List in Java

正如标题所暗示的,我试图在java中实现一个双向链表。

然而,在处理它时,我遇到了一些我难以理解的事情。

上下文是我的代码:

public class Doublelist{ 
    Node head; 
    public static class Node{   
        private int data;
        private Node prev; 
        private Node next = null; 
        public Node (int data){
            this.data = data;  
            prev = null;
            next = null; //default case for new node
        }   
        public String toString(){  
            String i = Integer.toString(data);
            return i;
        }    

//Method below handles inputting data at end of the linked list
    public static Doublelist add_node_end(Doublelist list, int data){  
        Node add_node = new Node(data);  
        add_node.next = null; 
        if (list.head == null){ 
            list.head = add_node;  
            list.head.prev = null;  
        } 
        else{  
            Node travel = list.head;    
            while (travel.next != null){   
                travel = travel.next; 
            }      
           add_node.prev = travel;
           travel.next = add_node;   
        }
        return list;
    }  


    public static void modify_obj_test (Doublelist list){  
        Node travel  = list.head;     
        System.out.println("Travel initially: "+ travel); 
        Node currnode = travel;  
        travel.next.next = null;
        System.out.println("Travel.next.next: "+ travel.next.next);  
        System.out.println("Currnode.next.next: "+ currnode.next.next); 

    }

    }   

这里是显示我遇到的问题的方法: (注意:我创建这个方法只是为了说明我的理解有问题,对链表没有任何作用)

    public static void modify_obj_test (Doublelist list){  
        Node travel  = list.head;     
        System.out.println("Travel initially: "+ travel); 
        Node currnode = travel;  
        travel.next.next = null;
        System.out.println("Travel.next.next: "+ travel.next.next);  
        System.out.println("Currnode.next.next: "+ currnode.next.next); 

    }

此输出给出以下内容

Travel initially: 1
Travel.next.next: null
Currnode.next.next: null

然而,当我更改此功能并执行以下操作时

    public static void modify_obj_test (Doublelist list){  
        Node travel  = list.head;     
        System.out.println("Travel initially: "+ travel); 
        Node currnode = travel;   
        travel = travel.next; 
        travel = travel.next; 
        travel = null;
        System.out.println("Travel.next.next: "+ travel);  
        System.out.println("Currnode.next.next: "+ currnode.next.next); 
    }

然后输出变成

Travel initially: 1
Travel.next.next: null
Currnode.next.next: 3

我的问题是,为什么当我使 travel.next.next = null 时 currnode.next.next 被修改,但是当我通过 travel = [=39 将列表逐个向下移动时=], 它不影响 currnode 变量?

我认为通过使 currnode = travel 有点像在 C 中创建一个临时变量,其中值的副本在操作进行时保持安全,我没有意识到这取决于我如何修改travel 变量它也可以影响 currnode 变量。

Why is it that the currnode.next.next is being modified when I make travel.next.next = null

如果您指的是第一组代码,currnode 指向与 travel (list.head) 相同的列表,并且由于您将 travel.next.next 设置为null,currnode.next.next 也是 null(因为它们都指向同一个列表)。

travel.next.next = null;

上面一行更改了列表中的数据。

however when I shift down the list one by one by doing travel = travel.next, It doesn't effect the currnode variable?

在第二组代码中,您没有将 travel.next.next 设置为 null。您正在将 travel 设置为空。因此,travel 不再指向任何内容,您也没有修改列表。

travel = travel.next;  // Move 'travel' to the next node
travel = travel.next;  // Move 'travel' to the next node
travel = null;  // Set 'travel' so that it is pointing to nothing

None 以上行更改列表中的数据。