[英]Finding an object recursively, returns null when found
I have created a recursive method for finding jcomponents by name. 我已经创建了一个通过名称查找jcomponents的递归方法。 This method finds the correct component, but it returns
null
. 此方法查找正确的组件,但它返回
null
。 I'm guessing I'm not handling the return of the component and the return null properly. 我猜我没有正确处理组件的返回和返回null。 How can I make this work properly?
我怎样才能正常工作?
edit: changed it to, what I understood from the below comments. 编辑:改为,我从下面的评论中理解。 But It won't return the Component.
但它不会返回组件。
public Component findComponent(String str, Component tt){
for (Component c : ((Container) tt).getComponents()) {
System.out.println("name: " + c.getName());
if(c.getName().equals(str)){
System.out.println("Found it! " + c.getName());
return c;
} else {
return findComponent(str, c);
}
}
return null;
}
This will just stop immediatly. 这将立即停止。 There's a
Component
which has no Components
so I'm guessing it will just stop immediatly and return null? 有一个
Component
没有Components
所以我猜它会立即停止并返回null?
if I remove the return
from findComponent(str, c);
如果我从
findComponent(str, c);
删除return
findComponent(str, c);
the console gives: 控制台给出:
name: titel
name: l
name: jpj
name: jtx
name: jpath
Found it! jpath
name: knapper
name: k1
name: n1
name: k2
name: n2
name: k3
name: n3
name: jpp
name: text
name: jpe
name: ta
title is the one which does not contain any components. title是不包含任何组件的title。 Is this a new question?
这是一个新问题吗?
Your else
block needs to return
what you recurse. 你的
else
块需要return
你递归的内容。 Something like, 就像是,
} else {
return findComponent(str, c);
}
Your else
block should be: 你的
else
块应该是:
else {
Component sub = findComponent(str, c);
if (sub != null) return sub;
}
Otherwise you'll only be checking your first component and only its first subcomponent and only is first sub-subcomponent and so on. 否则,您将只检查您的第一个组件,只检查其第一个子组件,并且只检查第一个子组件,依此类推。
在你的else
块中,你还需要一个return
语句:
return findComponent(str, c);
This is because you are returning null at the very end.. remove that line and change that loop to return the recursive call.. in the else.. 这是因为你在最后返回null ..删除该行并更改该循环以返回递归调用..在else ..
if(c.getName() != null){
if(c.getName().equals(str)){
System.out.println("Found it! " + c.getName());
return c;
} else {
return findComponent(str, c);
}
}
Put your return null;
把你的
return null;
statement in your else
block of code, something like these: 你的
else
代码块中的语句,如下所示:
} else {
findComponent(str, c);
return null;
}
Expanding on my comment to your question, I would try something like this: 扩展我对你的问题的评论,我会尝试这样的事情:
public Component findComponent(String str, Component tt){
if ( tt instanceOf Container )
for (Component c : ((Container) tt).getComponents()) {
System.out.println("name: " + c.getName());
if(c.getName().equals(str)){
System.out.println("Found it! " + c.getName());
return c;
} else {
Component c = findComponent(str, c);
if ( c != null ) return c;
}
return null;
}
else if(c.getName().equals(str)){
System.out.println("Found it! " + c.getName());
return c;
}
else
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.