Java 如何从返回值的函数中不返回任何内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20622156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return nothing from a function that returning value?
提问by Billie
I have a binary search tree, and I want to delete a node.
我有一个二叉搜索树,我想删除一个节点。
I need to get its parent, so I wrote a function:
我需要得到它的父级,所以我写了一个函数:
private BSTreeNode<T> getParent(BSTreeNode<T> root, BSTreeNode<T> node) {
if(root == null)
return null;
if(node.element().lessThan(root.element())) {
if(root.getLeft() != null && root.getLeft().element().equal(node.element()))
return root;
else
getParent(root.getLeft(), node);
} else {
if(root.getRight() != null && root.getRight().element().equal(node.element()))
return root;
else
getParent(root.getRight(), node);
}
}
Unlike C/C++, Java is giving me the following error:
与 C/C++ 不同,Java 给了我以下错误:
This method must return a result of type BSTreeNode<T>
It forces me to return a value in the last line of the function.
它迫使我在函数的最后一行返回一个值。
How can I fix it in the context of my function?
如何在我的函数上下文中修复它?
采纳答案by DoubleDouble
Your function does not have a return for every possible circumstance. You have:
您的函数不会在所有可能的情况下返回。你有:
if (null)...
if (less than root)...
else ( if ...)
else (no return!)
What do you return if it is not null, and goes to the final else? Nothing is returned.
如果它不为空,你会返回什么,并进入最后的 else?什么都没有返回。
You can either return getParent...
in the else statement. or return null
at the end of the function (not in an if or else statement)
您可以return getParent...
在 else 语句中。或return null
在函数的末尾(不在 if 或 else 语句中)
I often see code like so to cover the event of neither if statement returning a value.
我经常看到这样的代码来覆盖 if 语句都没有返回值的事件。
public int getAnswer()
{
if (answer.equals("yes"))
return 0;
else if (answer.equals("no"))
return 1;
return null;
}
回答by Samuel
You should be good with this:
你应该很擅长这个:
private BSTreeNode<T> getParent(BSTreeNode<T> root, BSTreeNode<T> node) {
if(root == null)
return null;
if(node.element().lessThan(root.element())) {
if(root.getLeft() != null && root.getLeft().element().equal(node.element()))
return root;
else
return getParent(root.getLeft(), node);
} else {
if(root.getRight() != null && root.getRight().element().equal(node.element()))
return root;
else
return getParent(root.getRight(), node);
}
}
回答by Hussein Terek
Add return null; statement at the end of the method . I think this statement will be unreachable in normal cases but add it just to fix the compilation error and try what will happen.
添加返回空值;方法末尾的语句。我认为这个语句在正常情况下是无法访问的,但添加它只是为了修复编译错误并尝试会发生什么。
private BSTreeNode<T> getParent(BSTreeNode<T> root, BSTreeNode<T> node) {
if(root == null)
return null;
if(node.element().lessThan(root.element())) {
if(root.getLeft() != null && root.getLeft().element().equal(node.element()))
return root;
else
getParent(root.getLeft(), node);
} else {
if(root.getRight() != null && root.getRight().element().equal(node.element()))
return root;
else
getParent(root.getRight(), node);
}
return null;
}