【问题标题】:Assign Values to Variable in Java在 Java 中为变量赋值
【发布时间】:2018-08-05 09:43:23
【问题描述】:

我正在尝试了解这是如何工作的

public class LinkedListMultiset<T> extends Multiset<T> {
private Node<T> head;

public LinkedListMultiset() {
    this.head = null;
} // end of LinkedListMultiset()


public void add(T item) {
    Node<T> node = new Node(item);

    if (head == null) {
        head = node;
    } else {
        Node<T> currentNode = head;
        while (currentNode.getNext() != null) {
            //-----------------------------------
            currentNode = currentNode.getNext();
            //-----------------------------------

        }
        currentNode.setNext(node);
    }
}

}

在 currentNode 被分配给当前节点中的下一个节点时,该节点本身不应该是一个全新的节点。但由于某种原因,它被附加到列表中?

【问题讨论】:

  • shouldnt that node be a completely new node in itself. but for some reason it is attach to the list 中不清楚你在问什么。新项目是一个新节点,就像这里所做的那样 - Node&lt;T&gt; node = new Node(item);它附加到尾部,以便在遍历列表时可以到达。

标签: java memory memory-management


【解决方案1】:

此代码尝试遍历列表直到最后一个节点。当currentNode.getNext() == null 到达最后一个节点时。所以currentNode.setNext(node); 被调用了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-18
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    • 1970-01-01
    相关资源
    最近更新 更多