对象在没有该对象引用的情况下得到更新?

Object is getting updated without that object reference?

//I have a Node.java Class

public class Node{

    int data;
    Node next;

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

//And another java class

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;
        }
    }
}

My Question is here , when temp1.next = newNode; line execute head object have added one next value.

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

"You" 确实有 head 元素。

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

所以,添加这样的作品:

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

要理解的就这些了。或者让我们尝试一些非 IT 示例。

假设你有一些钩子和短绳;并且您想构建一个 "list of ropes".

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

希望对您有所帮助。

您没有更新头对象。 您正在更新 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();
    }
}

节点 class 带有额外的 toString() 以正确查看对象。

public class Node {

    int data;
    Node next;

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

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