Results 1 to 4 of 4
  1. #1
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default 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:

    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();
        	}
        }
    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.

    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.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    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.

  3. #3
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Cool Thank You Junky, that's all I needed to know

  4. #4
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    I guess you could throw an exception... poor practice, but it should work.

    But why don't you just write code to recurse back up the stack without doing any more processing (e.g. tail recursion)?

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Replies: 11
    Last Post: 06-28-2011, 12:47 AM
  3. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM
  4. Replies: 4
    Last Post: 04-03-2009, 06:36 AM
  5. Replies: 8
    Last Post: 03-03-2009, 08:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •