gpt4 book ai didi

Java 递归 : pass by reference

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:31:29 24 4
gpt4 key购买 nike

我意识到这是 Java 程序员热议的、有争议的话题,但我相信我的问题有些独特。我的算法REQUIRES 通过引用传递。我正在对一棵通用树(即 n-children)进行顺时针/逆时针预排序遍历以分配虚拟 (x,y) 坐标。这只是意味着我在访问它们时计算(并标记)我访问的树的节点。

/**
* Generates a "pre-ordered" list of the nodes contained in this object's subtree
* Note: This is counterclockwise pre-order traversal
*
* @param clockwise set to true for clockwise traversal and false for counterclockwise traversal
*
* @return Iterator<Tree> list iterator
*/
public Iterator<Tree> PreOrder(boolean clockwise)
{
LinkedList<Tree> list = new LinkedList<Tree>();
if(!clockwise)
PreOCC(this, list);
else
PreO(this,list);
count = 0;
return list.iterator();
}
private void PreOCC(Tree rt, LinkedList<Tree> list)
{
list.add(rt);
rt.setVirtual_y(count);
count++;
Iterator<Tree> ci = rt.ChildrenIterator();
while(ci.hasNext())
PreOCC(ci.next(), list);
}
private void PreO(Tree rt, LinkedList<Tree> list, int count)
{
list.add(rt);
rt.setX_vcoordinate(count);
Iterator<Tree> ci = rt.ReverseChildrenIterator();
while(ci.hasNext())
PreO(ci.next(), list, ++count);
}

这里我生成了树的结构:

Tree root = new Tree(new Integer(0));
root.addChild(new Tree(new Integer(1), root));
root.addChild(new Tree(new Integer(2), root));
root.addChild(new Tree(new Integer(3), root));
Iterator<Tree> ci = root.ChildrenIterator();
ci.next();
Tree select = ci.next();
select.addChild(new Tree(new Integer(4), select));
select.addChild(new Tree(new Integer(5), select));

这是我打印遍历节点的顺序及其分配给相应节点的坐标时的输出。

0 3 2 5 4 1
0 1 2 3 4 3

0 1 2 4 5 3
0 1 2 3 4 3

注意:前两行是顺时针的先序遍历和x坐标的赋值。接下来的两行是逆时针预序遍历和 y 坐标赋值。

我的问题是如何读取第二行:0 1 2 3 4 5

编辑 1:这是我用来打印我访问节点的顺序和我分配的坐标的代码。

Iterator<Tree> pre = root.PreOrder(true);
System.out.println(" \t");
while(pre.hasNext())
System.out.print(pre.next() + "\t");

pre = root.PreOrder(true);
System.out.println();
System.out.println("x-coordinates:\t");
while(pre.hasNext())
System.out.print(pre.next().getVirtual_x() + "\t");

System.out.println();
System.out.println();

Iterator<Tree> preCC = root.PreOrder(false);
System.out.println(" \t");
while(preCC.hasNext())
System.out.print(preCC.next() + "\t");

preCC = root.PreOrder(false);
System.out.println();
System.out.println("x-coordinates:\t");
while(preCC.hasNext())
System.out.print(preCC.next().getVirtual_y() + "\t");

这里还有一段引述,可以更好地解释 x,y 坐标。顶点。顶点的 y 坐标。

Compute the counterclockwisepre-ordering of the vertices of T (theordering are numbered from 0 to n −1), use them as the x-coordinates forthe vertices.

Compute the clockwise pre-ordering ofthe vertices of T (the ordering arenumbered from 0 to n − 1), use them asthe y-coordinates for the vertices.

最佳答案

Java 始终按值传递 - 对于基元和对象。它是为非基元传递的引用,因此您可以更改它们指向的对象的状态,但不能更改引用本身。

摘自 James Gosling 在“The Java Programming Language”中:

"...There is exactly one parameter passing mode in Java - pass by value - and that keeps things simple. .."

我认为这是对此的最终权威。

I realize this is a hotly debated, controversial topic for Java programmers

不,没有争论。 James Gosling 从一开始就将其植入语言中。如果您认为它有争议,那您可悲的是被迷惑了或无知了。

关于Java 递归 : pass by reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4060509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com