简体   繁体   English

退货声明不起作用!

[英]Return statement does not work!

I have this simple method here: 我在这里有这个简单的方法:

private Node addItem(Node current, Node target) {
    if (current.data.getId() < target.data.getId()) {
        if (current.larger == null) {
            current.larger = target;
            Log.i("BinaryTree", "Added item: " + target.data.getId());
            return target;
        }
        return addItem(current.larger, target);
    } else {
        if (current.smaller == null) {
            current.smaller = target;
            Log.i("BinaryTree", "Added item: " + target.data.getId());
            return target;
        }
        return addItem(current.smaller, target);
    }
}

when i debug it, the code gets to the line 'return target;', and just skips it and goes to the last return statement - 'return addItem(current.smaller, target);'! 当我调试它时,代码到达“ return target;”行,并跳过它并转到最后一个return语句-“ return addItem(current.smaller,target);”! I have never in my life seen anything like this WTF?!?! 我一生中从未见过像这样的WTF吗?

You probably saw your debug jump 'back' one method. 您可能会看到调试跳回“一种”方法。

You're calling the addItem recursive; 您正在调用addItem递归; so the final return, where it actually will add it and return back up; 所以最终的回报,实际上是它将添加并返回的地方; will 'seem' to jump to another return, just because the Method call you're returning from originated there. 会“似乎”跳到另一个返回,只是因为您要从中返回的Method调用起源于此。

If it's reaching that return statement then it should definitely be returning from that method. 如果到达该return语句,则它肯定应该从该方法返回。 If you can't tell (since it's recursive) try placing a few System.out.println() statements. 如果您不知道(因为它是递归的),请尝试放置一些System.out.println()语句。

For example: 例如:

...
Log.i("BinaryTree", "Added item: " + target.data.getId());
System.out.println("Returning: " + target.toString());
return target;
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM