在我的原始代码中,我将节点添加到树中。我的目标是以某种方式访问我在树中添加的最后一个节点(我的想法是创建另一个指向最后一个对象的对象(我的原始示例中的节点))。
public class RandomSubClass
{
int firstNum;
}
import java.util.LinkedList;
public class RandomClass
{
LinkedList<RandomSubClass> myListOfObjects = new LinkedList<RandomSubClass>();
void addItem(int firstNum, RandomSubClass toBeReturned)
{
RandomSubClass o1 = new RandomSubClass();
o1.firstNum = firstNum;
myListOfObjects.add(o1);
// Here I was thinking that 'toBeReturned' will get same address as
// 'o1', and by changing 'toBeReturned' (in main), values in 'o1' change
//
toBeReturned = o1;
// This following commented code worked,
// but I can't use it in my original code.
//
// The reason I can't use it is because when I add a node to a tree,
// I start at the root and trace the new node's way to a new leaf,
// which makes it hard to do (simply) that.
//
//toBeReturned.firstNum = firstNum;
//myListOfObjects.add(toBeReturned);
}
}
public class Main
{
public static void main(String[] args)
{
RandomClass myList = new RandomClass();
RandomSubClass r1 = new RandomSubClass();
RandomSubClass r2 = new RandomSubClass();
myList.addItem(1, r1);
myList.addItem(2, r2);
// I would like to do that, and see changes in the node in 'myList'
//
r1.firstNum = 10;
r2.firstNum = 20;
}
}
在将节点添加到树后,我想检查有关该节点的某些内容,如果它满足某些条件,我想更改该节点的标志。
我可以再次重新跟踪节点(从根开始),但我的树可能会在某个时候变得很大,这需要时间。因此,如果我在添加该节点时获得了该节点的地址,并且在检查了我的条件之后,我可以修改该地址处的标志,知道它将更改该节点处的标志(最后添加)。