Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的回归并没有停止执行_Java_Return - Fatal编程技术网

Java 我的回归并没有停止执行

Java 我的回归并没有停止执行,java,return,Java,Return,我有一个返回,但是您可以看到行“this will not print”,在调用返回后不应到达该行 发生什么事了 这是整个过程,目前只是一个粗略的副本…: private void greedySearch (String lookForNode) { // Note: Available vars // reqStartNode // reqEndNode // Search through entire tree looking for... Sy

我有一个返回,但是您可以看到行
“this will not print”
,在调用
返回后不应到达该行

发生什么事了

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

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");

}
我的输出现在是:


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

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

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

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

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

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

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

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

我认为您的代码不会进入else块。这是一种递归方法吗?向我们展示一幅更大的图片。您应该使用逐步调试程序来准确了解执行的内容。尝试完整构建您的项目,可能执行的代码不是项目中的代码editor@gbhall:你懂递归吗?运行我在上面的评论中提供的代码,您将看到发生了什么。我认为您的代码不会进入else块。这是一个递归方法吗?向我们展示一个更大的画面。您应该使用逐步调试程序来准确理解执行的内容。尝试完整构建您的项目,可能执行的代码不是项目中的代码editor@gbhall:你懂递归吗?运行我在上面的注释中提供的代码,您将看到发生了什么。我忘了它是递归的。:)谢谢你,我已经度过了漫长的一天。不假思索,有没有一种方法可以结束所有正在进行的递归调用?因为这个方法是
void
,也许你可以将它转换为
bool
,然后返回给它的调用者它是应该继续还是完全停止。我没在想,我明白了,只需在greedSearch(smallestNode)之后添加一个return像这样:
greedSearch(smallestNode);返回退出,因为我们现在正在进行递归,我们的工作是Done我忘记了它是递归的。:)谢谢你,我已经度过了漫长的一天。不假思索,有没有一种方法可以结束所有正在进行的递归调用?因为这个方法是
void
,也许你可以将它转换为
bool
,然后返回给它的调用者它是应该继续还是完全停止。我没在想,我明白了,只需在greedSearch(smallestNode)之后添加一个return像这样:
greedSearch(smallestNode);返回退出,因为我们现在是递归的,我们在这里的工作完成了