【问题标题】:Trouble Understanding Objects and Modifying Variables of them in Linked List in Java在 Java 的链表中无法理解对象和修改它们的变量
【发布时间】:2019-12-31 15:53:59
【问题描述】:

正如标题所暗示的,我试图在 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 = travel.next 将列表一一向下移动时, 不影响 currnode 变量?

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

【问题讨论】:

    标签: java object variables linked-list doubly-linked-list


    【解决方案1】:

    为什么我在make travel.next.next = null的时候currnode.next.next被修改了

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

    travel.next.next = null;
    

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

    但是,当我通过旅行将列表逐个向下移动时 = travel.next,不影响 currnode 变量?

    在第二组代码中,您没有将travel.next.next 设置为空。您正在设置 travel null。所以,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
    

    以上行都不会改变列表中的数据。

    【讨论】:

    • 非常感谢您的解释。当 travel = travel.next 而不是 travel.next.next 时,我有点知道对于 travel 变量正在做某事,但我就是无法清楚地表达或理解到底发生了什么。再次感谢您!
    猜你喜欢
    • 2016-05-10
    • 2016-01-15
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    • 2012-09-12
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多