0

我有一个返回,但是你可以看到发生调用"this will not print"后不应该到达的行return

这是怎么回事?

代码

这是整个过程,目前是一个粗略的副本......:

private void greedySearch (String lookForNode)
{
    // Note: Available vars
    // reqStartNode
    // reqEndNode

    // Search through entire tree looking for...
    System.out.println("Searching through entire tree looking for "+lookForNode);
    for (int i = 0; i < treeList.size(); i++) {

        Data currentNode = treeList.get(i);

        // ... reqStartNode
        if (currentNode.getNodeName().equals(lookForNode))
        {   
            System.out.println("Found matching node. currentNode.getNodeName=" + currentNode.getNodeName()+" lookForNode="+lookForNode);

            // Check to see if there's any children?
            if (currentNode.childrenList.size() > 0)
            {
                // Find smallest child by node
                double smallestHeuristic = currentNode.childrenList.get(0).getHeuristic();
                String smallestNode = currentNode.childrenList.get(0).getNodeName();
                for (int ii = 1; ii < currentNode.childrenList.size(); ii++)
                {
                    if (currentNode.childrenList.get(ii).getHeuristic() < smallestHeuristic)
                    {
                        smallestHeuristic = currentNode.childrenList.get(ii).getHeuristic();
                        smallestNode = currentNode.childrenList.get(ii).getNodeName();
                    }
                }

                // Check to see if smallest child by node is reqEndNode
                if (smallestNode == reqEndNode)
                {
                    System.out.println("FOUND GOAL "+smallestNode);

                    // Quit because we found the answer
                    return;
                }
                // Expand that node
                else
                {
                    greedySearch (smallestNode);
                }
            }
            // No children, we've reached the end
            else
            {
                System.out.println("We've reached the end at "+currentNode.getNodeName());

                // Quit because we've reached no further children to expand
                return;
            }
            System.out.println("This will not print");      
        }
        else
        {
            System.out.println("Skipped node "+currentNode.getNodeName());
        }
    }

    System.out.println("FINISHED SEARCH");

}

编辑:

我意识到的正确解决方案是return在我调用递归过程之后执行以下操作:

greedySearch (smallestNode);
// Quit because we are now going recursive, our job here is done
return;

我的输出现在是:

Searching through entire tree looking for S
Skipped node A
Skipped node B
Skipped node C
Skipped node D
Skipped node E
Skipped node F
Skipped node G
Skipped node G
Found matching node. currentNode.getNodeName=S lookForNode=S
Searching through entire tree looking for A
Found matching node. currentNode.getNodeName=A lookForNode=A
Searching through entire tree looking for B
Skipped node A
Found matching node. currentNode.getNodeName=B lookForNode=B
Searching through entire tree looking for C
Skipped node A
Skipped node B
Found matching node. currentNode.getNodeName=C lookForNode=C
We've reached the end at C
4

1 回答 1

4

没有什么奇怪的事情发生。我可以看到至少一个可能发生这种情况的代码路径。

在嵌套调用中,执行以下操作:

        else
        {
            System.out.println("We've reached the end at "+currentNode.getNodeName());

            // Quit because we've reached no further children to expand
            return;
        }

然后返回到外部调用:

            else
            {
                greedySearch (smallestNode); // Resuming from here...
            }
        }
        else
        {
            // ...all this is skipped (because we are in the else block
            // of an if that was true)...
        }
        // ...and this is printed.
        System.out.println("This will not print");      
    }

换句话说,虽然您正在查看的这两行在递归方法的一次调用期间确实是互斥的,但它们在两个嵌套调用之间并不互斥。他们打印的消息可以按顺序出现,就像您的输出一样。

于 2013-05-04T10:20:04.663 回答