Java 带循环和中断的递归
我正在设计一个递归方法,它有一个for循环和break内部,并期望在满足一定条件后中断循环。 下面是一段代码Java 带循环和中断的递归,java,Java,我正在设计一个递归方法,它有一个for循环和break内部,并期望在满足一定条件后中断循环。 下面是一段代码 public static UIComponent findElem(UIComponent component){ UIComponent comp = null; for(UIComponent child : component.getChildren()){ if(child instanceof RichSelectBooleanRadio){ Rich
public static UIComponent findElem(UIComponent component){
UIComponent comp = null;
for(UIComponent child : component.getChildren()){
if(child instanceof RichSelectBooleanRadio){
RichSelectBooleanRadio radioButton = (RichSelectBooleanRadio)child;
System.err.println("radioButton:: + " + radioButton);
Object val = radioButton.getValue();
if(val == null){
val = radioButton.getSubmittedValue();
}
System.err.println("val ::" + val);
if( val != null && Boolean.parseBoolean(val.toString())){
comp = child;
}
break;
}
findElem(child);
}
在此代码中,循环不会在中断后终止。
有人能帮我找出这个问题吗
提前谢谢 试试这样的方法:
public static UIComponent findElem(final UIComponent component)
{
for (final UIComponent child : component.getChildren())
{
if (child instanceof RichSelectBooleanRadio)
{
final RichSelectBooleanRadio radioButton = (RichSelectBooleanRadio) child;
System.err.println("radioButton :: + " + radioButton);
Object val = radioButton.getValue();
if (null == val)
val = radioButton.getSubmittedValue();
System.err.println("val :: " + val);
if (null != val && Boolean.parseBoolean(val.toString()))
return child;
}
else
{
// Use the result of the recoursive call: if not NULL, return it
final UIComponent comp = findElem(child);
if (null != comp)
return comp;
}
}
// Return NULL if the loop ended without early return
return null;
}
你凭什么认为它不会终止?考虑重新编写代码输入,并显示实际输出,并告诉我们您期望的是什么。因此,核心转储在哪里?因为FEXELEM在外面,它将不会停止在断点处递归,您错过了返回GHOTCAT的某个地方,我期待组件一旦条件为真。请看,我修改了代码,并在满足条件后返回组件。但它仍然不起作用。由于您删除了
中断
,您只是使大部分问题文本和标题无效。您更改了什么?你能突出显示和/或解释这些变化吗?谢谢Usagi,我实现了它,它工作得很好。