在Java中为变量赋值
我正在努力理解这是如何工作的在Java中为变量赋值,java,memory,memory-management,Java,Memory,Memory Management,我正在努力理解这是如何工作的 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 =
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);
}
}
}
公共类LinkedListMultiset扩展了Multiset{
专用节点头;
公共LinkedListMultiset(){
this.head=null;
}//LinkedListMultiset()的结尾
公共作废新增(T项){
节点=新节点(项目);
if(head==null){
头部=节点;
}否则{
节点当前节点=头;
while(currentNode.getNext()!=null){
//-----------------------------------
currentNode=currentNode.getNext();
//-----------------------------------
}
currentNode.setNext(节点);
}
}
}
在将currentNode分配给当前节点中的下一个节点时,该节点本身不应是一个全新的节点。但是由于某种原因,它被附加到列表?此代码尝试遍历列表,直到最后一个节点。当
currentNode.getNext()==null
时,我们已到达最后一个节点。SocurrentNode.setNext(节点)调用code>。在中,该节点本身不应该是一个全新的节点。但由于某种原因,它被附加到了列表中
,不清楚你在问什么。新项目是一个新节点,如下所示-node node=new node(项目)代码>并将其附加到尾部,以便在遍历列表时可以到达。