简体   繁体   English

包含for循环返回语句问题的递归方法

[英]Recursive method containing for loop return statement issue

This is for a homework assignment. 这是用于家庭作业。 The homework is not on recursion it is on tree structures. 作业不是在递归上,而是在树结构上。 I have nearly finished the assignment, but my recursive method for moving back up a tree breaks. 我的任务差不多完成了,但是我递归的方法打破了。 The tree structure is given by the class below: 树结构由以下类给出:

package lab12;

import java.io.Serializable;

public class Dog implements Serializable{

    public Dog[] children;
    public String name;

    public Dog(String name)
    {
        this.name = name;
    }

    @Override
    public String toString()
    {
        return name;
    }

} 

I'm quite certain the reason is the return null; 我很确定原因是返回null。 statement in combination with my for loop. 语句与我的for循环结合使用。 The for loop iterates over a node that doesn't contain any children and as a result returns null. for循环遍历不包含任何子节点的节点,结果返回null。 This ends the method and passes back null to my program which gives me null pointer exceptions. 这样就结束了该方法,并将null传递回我的程序,这给了我null指针异常。

I can't remove the return null statement or it won't compile, even though it will 100% return using the for loop. 我无法删除return null语句,也不会编译,即使使用for循环将100%返回也是如此。

public Dog findParent(Dog root, String name)
{
    String top = "Spot";
    if(top.equals(name))
    {
        System.out.println("No further records");
        System.out.println("Goodbye.");
        System.exit(0);
    }
    for(int i = 0; root.children != null && i < root.children.length; i++)
    {
        if(root.children[i].name.equals(name))
        {
            return root;
        }
        else
        {
            return findParent(root.children[i], name);
        }
    }
    return null; //Compiler still requires a return here.
}

I feel like this has to be a common problem with using for loops in non-void recursive methods. 我觉得在非void递归方法中使用for循环必须是一个普遍的问题。 Is there a way to make the compiler happy and yet not have the extra return null statement? 有没有办法使编译器满意,但又没有多余的return null语句?

Your code must not work. 您的代码不能正常工作。 Because both if and else clauses will return. 因为if和else子句都会返回。 That causes the loop only do index 0. You should change your code like the following: 这将导致循环仅执行索引0。您应该像下面那样更改代码:

public Dog findParent(Dog root, String name)
{
    String top = "Spot";
    if(top.equals(name))
    {
        System.out.println("No further records");
        System.out.println("Goodbye.");
        System.exit(0);
    }
    for(int i = 0; root.children != null && i < root.children.length; i++)
    {
        if(root.children[i].name.equals(name))
        {
            return root;
        }
        else
        {
            Dog parent = findParent(root.children[i], name);
            if (parent != null) 
                 return parent;
        }
    }
    return null;
}

Now, you can see that the last "return null" is necessary. 现在,您可以看到最后一个“返回null”是必要的。

In most of case, compiler is smart. 在大多数情况下,编译器很聪明。 If it gives your warnings, you should consider what's error with your code instead just cheat compiler to avoid warning. 如果它发出警告,则应考虑代码的错误,而不是仅仅欺骗编译器以避免警告。

Without understanding this problem entirely, I see no reason for the "return null" statement to never execute. 如果不完全理解此问题,我看不出“ return null”语句永远不会执行的原因。 Perhaps you else statement should be: 也许您的其他声明应为:

return findParent(root.children[i], name);

This return will ensure that once the "parent" is found its value will be returned. 此返回将确保一旦找到“父”,其值将被返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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