2013-05-04 45 views
0

我有退貨,但是您可以看到行,在return發生調用後不應該達到該行。我的退貨不會停止執行

發生了什麼事?

Code

這裏的整個過程,這是目前一個粗略的副本...:

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 
+2

我認爲你的代碼不要輸入else塊。 – 2013-05-04 10:11:00

+0

這是一個遞歸方法嗎? – jlordo 2013-05-04 10:11:52

+0

向我們展示整個代碼。 – 2013-05-04 10:12:06

回答

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

換句話說,當你看着這兩條線是在遞歸方法的一次調用中,它確實是相互排斥的,它們在兩個嵌套調用之間並不相互排斥。他們打印的信息可以按順序出現,就像輸出中的情況一樣。

+0

我忘了它是遞歸的。 :)謝謝,我度過了漫長的一天。 – gbhall 2013-05-04 10:25:42

+0

沒有想到,有沒有辦法結束它的所有遞歸調用的方式? – gbhall 2013-05-04 10:28:30

+1

由於這個方法是'void',所以也許你可以把它變成一個'bool'並返回給它的調用者,無論它應該繼續還是完全停止。 – 2013-05-04 10:30:20