Java 递归 : pass by reference

标签 java pass-by-reference graph-theory pass-by-value

我意识到这是 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 counterclockwise pre-ordering of the vertices of T (the ordering are numbered from 0 to n − 1), use them as the x-coordinates for the vertices.

Compute the clockwise pre-ordering of the vertices of T (the ordering are numbered from 0 to n − 1), use them as the 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/

相关文章:

java - Maven Java 编译太慢

java - 在 Java 中转换对象

c++ - 指向引用的指针是非法的

java - java中泛型类中的非泛型构造函数

algorithm - 计算图中三角形数量的有效算法是什么?

java - 在黑莓中读/写文件

java - 将线程用于 100 个独立的小型本地搜索作业是否是一种干净的方法?

c++ - 通过引用传递 vector 的尾递归

c++ - Qt 如何处理信号和槽的引用调用?

algorithm - 多个目的地的最高加权路径