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", 开发者_如何学Python"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;
...
精彩评论