简体   繁体   中英

Finding an object recursively, returns null when found

I have created a recursive method for finding jcomponents by name. This method finds the correct component, but it returns null . I'm guessing I'm not handling the return of the component and the return null properly. 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?

if I remove the return from 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. Is this a new question?

Your else block needs to return what you recurse. Something like,

} else {
    return findComponent(str, c);
}

Your else block should be:

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..

 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; statement in your else block of code, something like these:

} 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM