简体   繁体   中英

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);'! I have never in my life seen anything like this WTF?!?!

You probably saw your debug jump 'back' one method.

You're calling the addItem recursive; 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.

If it's reaching that return statement then it should definitely be returning from that method. If you can't tell (since it's recursive) try placing a few System.out.println() statements.

For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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