Results 1 to 4 of 4
- 07-04-2011, 12:31 AM #1
Q regarding Method within Methods
Hello Java Friends
Can someone please tell me whether it is possible not to come back to a certain place in code, once a condition is met ?
Take the following code for example:
Bear with me a second, it might seem daunting at first, but my question is a general one. As you can see, in the above code, there are a number of recursive calls to the method. These calls get "piled" up on top of one another, and each carries one, once a specific run is done.Java Code:public void getAvailablePaths(Node<Adjustment> x) throws BotError { // Local Variable [p] to hold the current Stacked Path. ArrayDeque<Node<Adjustment>> p = new ArrayDeque<Node<Adjustment>>(); if(paths.isEmpty()) // If true, this would indicate that [x] is an Entrance node. { for(Node<Adjustment> n : g.get(x)) // for each neighbours of [x] { if(!nodesUsed.contains(n.getName())) { p.addLast(x); // All our paths need to start with the Entrance node. p.addLast(n); // now adding neighbouring node. // Adding the stacked path [p] to the safe paths list [paths] paths.add(p); nodesUsed.add(n.getName()); // Now that the stacked path [p] has been stored, we need a new empty [p] for the next path. p = new ArrayDeque<Node<Adjustment>>(); } } nodesWithInspectedNeighbours.add(x); } else { if(!(x instanceof ExitNode)) { for(Node<Adjustment> n : g.get(x)) { if(n != x) // This is to check for own-looping. (e.g. A path from Node A to Node A.) { if(!nodesWithInspectedNeighbours.contains(n)) { // traditional for loop to iterate over each stack in [paths] int pathsSize = paths.size(); for(int i=0; i < pathsSize; i++) { if(paths.get(i).peekLast() == x) { ArrayDeque<Node<Adjustment>> ArrayD = new ArrayDeque<Node<Adjustment>>(); // I now have hold of a path stack, e.g. [A, B] ArrayD.addAll(paths.get(i)); // Adding the new Node to the Path, so the temporary stack path becomes >> [A, B, F] ArrayD.addLast(n); // Adding the temporary stacked path to the safe paths list [paths] paths.add(ArrayD); nodesUsed.add(n.getName()); } } } } } nodesWithInspectedNeighbours.add(x); } } Node<Adjustment> node = null; // Recursion. for(Node<Adjustment> n : g.get(x)) // for each neighbours of [x] { // check for own loop - maybe if(!nodesWithInspectedNeighbours.contains(n) && !(n instanceof ExitNode)) { node = n; break; } else if (isNeighbourOfExit(n) && !nodesWithInspectedNeighbours.contains(n)) // that neighbour has a neighbour of its own that's of type ExitNode. { if(!(n instanceof ExitNode)) { node = n; break; } } } if(node != null) { getAvailablePaths(node); } Enumeration<Node<Adjustment>> e = g.keys(); while(e.hasMoreElements() && (getMessage() == null)) { Node<Adjustment> xy = (Node<Adjustment>)e.nextElement(); if(xy instanceof EntryNode) { Node<Adjustment> node2 = null; for(Node<Adjustment> na : g.get(xy)) { if(!nodesWithInspectedNeighbours.contains(na) && (!(na instanceof ExitNode))){ node2 = na; break; } } if(node2 != null) { getAvailablePaths(node2); } } } if ((nodesWithInspectedNeighbours.size() == (g.size() - 1)) && (getMessage() == null)) // checking that neighbours of all the nodes have been inspected. { FindOptimumPath(); } if(getMessage() == null) { for(ArrayDeque<Node<Adjustment>> ad : paths) { if(!(ad.peekLast() instanceof ExitNode)) { ArrayList<Node<Adjustment>> al = new ArrayList<Node<Adjustment>>(); al.addAll(ad); Node<Adjustment> oneBeforeLast = al.get(al.size() -1); Node<Adjustment> node3 = null; for(Node<Adjustment> nod : g.get(oneBeforeLast)) { if(!nodesWithInspectedNeighbours.contains(nod)) { node3 = nod; break; } } if(node3 != null) { getAvailablePaths(node3); } } } FindOptimumPath(); } }
My question is, can I (after reaching FindOptimumPath(), which leave a Message object) stop the recursion, and stop the code from going back through the piled up runs ?
I hope it is clear what I mean, if not let me know and I'll try and explain again.
Thank You.>> What can be asserted without proof can be dismissed without proof. <<
- 07-04-2011, 01:18 AM #2
No it is not possible. Method calls are placed on a Stack which means you cannot access the previous method until the current one has completed and been removed from the Stack.
- 07-04-2011, 02:13 AM #3
Cool Thank You Junky, that's all I needed to know
>> What can be asserted without proof can be dismissed without proof. <<
- 07-04-2011, 02:16 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
putting the output of different methods inside a method automatically
By eagle26 in forum New To JavaReplies: 11Last Post: 06-28-2011, 12:47 AM -
ArrayLists compareTo method, equals method
By random0munky in forum New To JavaReplies: 2Last Post: 10-26-2009, 07:20 PM -
How to get methods to see variables in other methods
By ejs7597 in forum New To JavaReplies: 4Last Post: 04-03-2009, 06:36 AM -
How to Cal methods within a method in the same class
By Manfizy in forum New To JavaReplies: 8Last Post: 03-03-2009, 08:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks