2013-05-04 1 views
0

반품이 있습니다. return 사건이 발생한 후에는 "this will not print"에 도달해서는 안되는 행을 볼 수 있습니다.내 수익은 실행을 중지하지 않습니다.

무슨 일 이니?

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; 

내 OUPUT는 지금 : 이상한

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

나는 당신의 코드가 다른 블록에 들어 가지 않을 것이라고 생각한다. –

+0

이것은 재귀 적 방법입니까? – jlordo

+0

전체 코드를 보여주십시오. –

답변

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

+0

생각이 없으면 방해가되는 모든 재귀 호출을 끝내는 방법이 있습니까? – gbhall

+1

이 메소드는'void'이므로 어쩌면 당신은'bool'으로 바꿀 수 있고 호출자가 완전히 계속할 지 또는 완전히 멈추어야하는지에 상관없이 호출자에게 되돌릴 수 있습니다. –

관련 문제