Note that there are some explanatory texts on larger screens.

plurals
  1. POMy return is not stopping execution
    primarykey
    data
    text
    <p>I have a return, however you can see the line <code>"this will not print"</code> which shouldn't be reached after the <code>return</code> happens gets called. </p> <p>What's going on?</p> <p><img src="https://i.stack.imgur.com/ef8KB.png" alt="Code"></p> <p>Here's the entire procedure, it's a rough copy at the moment...:</p> <pre><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 &lt; 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() &gt; 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 &lt; currentNode.childrenList.size(); ii++) { if (currentNode.childrenList.get(ii).getHeuristic() &lt; 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"); } </code></pre> <p><strong>Edit:</strong></p> <p>The correct solution I realised was doing a <code>return</code> after I call the recursive procedure like so:</p> <pre><code>greedySearch (smallestNode); // Quit because we are now going recursive, our job here is done return; </code></pre> <p>My ouput is now:</p> <blockquote> <pre><code>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 </code></pre> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload