在Java中,如果递归方法可以返回混合值,我应该如何声明返回类型?
我有下面的方法定义,它用于在JSON对象中搜索给定的键,并返回该键的JSONObject或字符串值。为了确保它搜索JSON对象的每一个级别,我将其设置为递归,但仅在可以返回更深层次的JSONObject的情况下。编译器抱怨这必须返回一个对象,因为我已经声明了返回类型。好的在两种情况下,我返回一个对象,但我认为它的问题是,在某些情况下,它不会返回任何东西。如果我添加了一个final return false或其他内容,它将通过编译器检查,但是对该方法的调用将始终(最终)返回false,使其无效。我不习惯像Java这样的严格类型语言,所以我以前从未遇到过类似的问题。任何指点都将不胜感激在Java中,如果递归方法可以返回混合值,我应该如何声明返回类型?,java,android,json,return,type-conversion,Java,Android,Json,Return,Type Conversion,我有下面的方法定义,它用于在JSON对象中搜索给定的键,并返回该键的JSONObject或字符串值。为了确保它搜索JSON对象的每一个级别,我将其设置为递归,但仅在可以返回更深层次的JSONObject的情况下。编译器抱怨这必须返回一个对象,因为我已经声明了返回类型。好的在两种情况下,我返回一个对象,但我认为它的问题是,在某些情况下,它不会返回任何东西。如果我添加了一个final return false或其他内容,它将通过编译器检查,但是对该方法的调用将始终(最终)返回false,使其无效。我
public Object find(String contentId, JSONObject node) {
JSONObject currentNode = (node != null) ? node : this.txtContent;
Iterator<?> nodeKeys = currentNode.keys();
while ( nodeKeys.hasNext() ){
try {
String key = (String) nodeKeys.next();
if (key.equals(contentId)) {
if (currentNode.get(key) instanceof JSONObject) {
return currentNode.getJSONObject(key);
} else {
return currentNode.getString(key);
}
} else if (currentNode.get(key) instanceof JSONObject) {
find(contentId, currentNode.getJSONObject(key));
}
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
}
公共对象查找(字符串contentId,JSONObject节点){
JSONObject currentNode=(节点!=null)?节点:this.txtContent;
迭代器nodeKeys=currentNode.keys();
while(nodeKeys.hasNext()){
试一试{
字符串键=(字符串)nodeKeys.next();
if(key.equals(contentId)){
if(currentNode.get(key)JSONObject实例){
返回currentNode.getJSONObject(键);
}否则{
返回currentNode.getString(键);
}
}else if(currentNode.get(key)JSONObject实例){
查找(contentId,currentNode.getJSONObject(key));
}
}捕获(JSONException e){
抛出新的运行时异常(e);
}
}
}
添加返回空值;在while循环下方如果该方法不断地点击else if,则该方法只会一直返回false,直到while循环条件结束,您到达“return false”
使用returnnull代替returnfalse;最后,在调用方法中,检查返回的对象是否为null,这意味着未找到任何内容让我们看看,您应该使用find调用返回的值,如果未找到,则返回null:
public Object find(String contentId, JSONObject node) {
JSONObject currentNode = (node != null) ? node : this.txtContent;
Iterator<?> nodeKeys = currentNode.keys();
while ( nodeKeys.hasNext() ){
try {
String key = (String) nodeKeys.next();
if (key.equals(contentId)) {
if (currentNode.get(key) instanceof JSONObject) {
return currentNode.getJSONObject(key);
} else {
return currentNode.getString(key);
}
} else if (currentNode.get(key) instanceof JSONObject) {
Object foundObj = find(contentId, currentNode.getJSONObject(key));
if (foundObj!=null) {
return foundObj;
}
}
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
return null;
}
公共对象查找(字符串contentId,JSONObject节点){
JSONObject currentNode=(节点!=null)?节点:this.txtContent;
迭代器nodeKeys=currentNode.keys();
while(nodeKeys.hasNext()){
试一试{
字符串键=(字符串)nodeKeys.next();
if(key.equals(contentId)){
if(currentNode.get(key)JSONObject实例){
返回currentNode.getJSONObject(键);
}否则{
返回currentNode.getString(键);
}
}else if(currentNode.get(key)JSONObject实例){
Object foundObj=find(contentId,currentNode.getJSONObject(key));
if(foundObj!=null){
返回foundObj;
}
}
}捕获(JSONException e){
抛出新的运行时异常(e);
}
}
返回null;
}
这个或类就是针对这种情况而设计的。看
用法:
Either<OneResultType,OtherResultType> result;
任一结果;
这避免了昂贵的检查。如果找不到对象,则返回null
。为什么该方法最终总是返回false?(请注意,在这里返回null
通常比返回false
更合适…)为什么不对find
递归调用返回的值执行任何操作?正如您所指出的,它总是返回false,因为我没有对find()返回的值执行任何操作。我明白了。我通常会返回对find的调用(例如return find()),但在本例中,这似乎会导致不同的返回类型问题。我想这就是让我失望的原因。谢谢你花时间来帮助我。没问题,我以前也遇到过这样的问题。我不知道那门课。我会调查的。非常感谢。