public class IntList {
private IntNode _head;
public IntList() {
_head = null;
}
}
我创建了一个名为IntList的类。 IntList包含一个搜索具有指定int值的IntNode对象的方法。这是方法:将一个变量分配给另一个变量后,为什么更改一个变量不会改变另一个?
public boolean findElementInList(int value) {
IntNode currentNode = this._head;
while (currentNode.getValue() != value &&
currentNode.getNext() != null) {
currentNode = currentNode.getNext();
}
return (currentNode.getValue() == value);
}
的方法完成后,原_head
实例变量是完整的 - 但为什么呢? currentNode
指向方法中的_head
(混叠),并且对currentNode
所做的每个更改也应反映在_head
(每次运行currentNode = currentNode.getNext();
时)。
这是getNext()
代码:
public IntNode getNext() {return _next;}
currentNode是头的另一个引用,并且您通过调用_head.getNext()将该引用更改为列表中的下一个元素,除非getNext()以某种方式修改头,否则头不会更改 – SomeDude
getNext() '回来?请包括'IntNode'的代码。 – activedecay