【问题标题】:Object is getting updated without that object reference?对象在没有该对象引用的情况下得到更新?
【发布时间】:2016-12-17 10:22:31
【问题描述】:

//我有一个Node.java类

public class Node{

    int data;
    Node next;

    public Node(int d) {
        data = d;
    }
}

//还有一个java类

class LinkedList {

    Node head;

    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        //Executing this loop
        for (int i = 0; i < 5; i++) {

            **list.add(i);**

        }
    }

     void add(int value){
        Node newNode = new Node(value);

        if(head == null )//Very first time its create the head object when i = 0
        {
            head = newNode;
        }else if(head.next == null){//This is for when i value is 1
            head.next  = newNode;
        }else{ //else part execute i >= 2
            //Created new node with head.next which mean value 1.And head is 0
            Node temp = head.next; 
            // Just need this object initialization for reference
            Node temp1 = newNode; 
             //Checking head.next is null or not if its null skip this loop execution
            while(temp != null)
            {
                temp1 = temp; 
                 temp = temp.next;
            }
            // Here we set newNode.next to null
            newNode.next = temp1.next; 
            temp1.next = newNode;
        }
    }
}

我的问题在这里,当 temp1.next = newNode;行执行头对象已添加 下一个值。

** //例如 if head = 0,head.next = 1 when temp1.next = newNode;行执行 head.next.next = 2 正在添加头部。当我们没有头对象引用时它是如何发生的。

【问题讨论】:

  • 请花点时间正确格式化您的问题。
  • 说真的:您希望我们帮助您;所以请您花一些时间来正确格式化您的问题。适当的缩进、格式化等。您知道,创建问题时会有一个预览

标签: java list data-structures singly-linked-list


【解决方案1】:

您没有更新头部对象。 您正在更新 head.next 对象。

所以

head.next.next

可以这样写:

Node nextFromHead = head.next; // nextFromHead is 1
Node nextFromNextFromHead = nextFromHead.next; // nextFromNextFromHead is 2

head.next.nextnextFromNextFromHead 是同一个对象,但它(即 2 的节点)与头节点没有任何直接连接。

我认为这将有助于更好地理解引用在 java 中的工作原理。

public class LinkedList {

    static Node head;

    public static void main(String[] args) {

        LinkedList list = new LinkedList();
        for(int i = 0; i < 5; i++)

            list.add(i);

        Node currentNode = head; // in java we don't need object initialization for reference. Node temp1; would work just fine

        System.out.println("==head node== " + currentNode);
        while(currentNode.next != null) {

            // here we increment
            currentNode = currentNode.next;

//            System.out.println("Last time we in here, next is null so print only current");
            System.out.println("==next node== " + currentNode);
        }
    }

    void add(int value){
        Node newNode = new Node(value);

        if(head == null )//Very first time its create the head object when i = 0
        {
            head = newNode;
        }else if(head.next == null){//This is for when i value is 1
            head.next  = newNode;
        }else{ //else part execute i >= 2
            //Created new node with head.next which mean value 1.And head is 0
            Node temp = head.next;
            // Just need this object initialization for reference
            Node temp1 = newNode;
            //Checking head.next is null or not if its null skip this loop execution
            while(temp != null)
            {
                temp1 = temp;
                temp = temp.next;
            }
            // Here we set newNode.next to null
            System.out.println("  ==temp1== " + temp1);// before
            newNode.next = temp1.next;
            temp1.next = newNode;
            System.out.println("  ==temp1== " + temp1);// and after
        }

        System.out.println("==current node== " + head);
        System.out.println();
    }
}

还有一个带有附加 toString() 的 Node 类,用于正确查看对象。

public class Node {

    int data;
    Node next;

    public Node(int d) {
        data = d;
    }

    @Override
    public String toString() {
        return "Node{" +
                "data=" + data +
                ", next=" + next +
                '}';
    }
}

【讨论】:

  • 好的。但是我在这里感到困惑,例如我有 MyClass 有 name 字段。 MyClass object1 = new MyClass(); object1.name = "object1";MyClass object2 = object1;现在 object2.name = "object2";所以现在如果我打印 object1.name 它将是“object1”而不是“object2”,如果我对这个例子的理解是正确的,那么它是如何发生在 Node 类的。
  • 如果我的理解不正确,请指正。谢谢
  • 如果您打印 object1.name,它将打印“object2”。 object1 和 object2 都指向同一个对象。
  • 好的现在明白了...所以 object1 和 object2 是同一个对象。任何此对象的任何更改都会影响另一个正确的对象。谢谢..
【解决方案2】:

“你”确实有 head 元素。

看看你的代码:你的 LinkedList 类有一个字段头;以及每当您调用列表的 add() 方法时;该字段可通过该方法访问。

所以,添加这样的作品:

  1. 如果没有设置头部,则创建一个新的
  2. 如果设置了头,但没有“下一个”,则创建下一个节点并链接到头
  3. 如果头部已设置,并且他的“下一个”已设置,那么您将继续检索下一个“下一个”......直到找到最后一个;没有下一个(还)...

这就是要了解的全部内容。或者让我们尝试一些非 IT 示例。

假设你有一些钩子和短绳;你想建立一个“绳索列表”。

  1. 还没有列表。你拿起第一根绳子,把它挂在钩子上。
  2. 第一根绳子,你的头,在那里。你添加另一根绳子,将它连接到第一根绳子的末端(可能打个结)
  3. 添加另一根绳子……从钩子开始,然后继续跟随绳子/结……直到你有一个松散的末端。

希望对您有所帮助。

【讨论】:

  • 是的,接受您的 cmets。我还是一头雾水。是的,我发现直到我没有下一个元素,我确实添加了 temp1.next = newNode;这里 head 对象如何获取新的下一个元素。
  • 我不确定你在问什么;我尝试了一个不同的例子;查看我的更新答案。
猜你喜欢
  • 2021-08-06
  • 1970-01-01
  • 2011-05-06
  • 2012-04-14
  • 1970-01-01
  • 2017-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多