微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java递归:通过引用传递

我意识到这对于 Java程序员来说是一个备受争议,备受争议的话题,但我相信我的问题有点独特.我的算法要求通过引用传递.我正在进行一般树(即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的“Java编程语言”:

“…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)从一开始就将这种语言融入到语言中.如果你认为这是有争议的,你会被遗憾或无知.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐