It's not a secret anymore!
The moose likes Beginning Java and the fly likes recursive method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "recursive method" Watch "recursive method" New topic

recursive method

Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61
Hi here I am again, I know how recursive method works but the problem I'm facing now is analyzing this recursive method. I'm just testing the method.

The output I have is

I'm trying to analyze how did I get that output, but it seems that I'm totally lost I can only analyze the output if I only have one method calling itself once but method calling itself twice-can't discover the process. Any help would be appreciated
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257

It hardly makes a difference even if a method calls itself n times.

Just imagine there is only 1 call. Resolve the complete stack flow of the that 1 call and get the result. Now for the second method, just add a call to each stack entry. Write it down and you ll get it, if not... please tell, I'll represent diagramatically.


Experience and talent are independent of age
Marcos R Oliveira
Ranch Hand

Joined: Apr 20, 2002
Posts: 51
Hi, Kalabaw.

I think you can have a better understanding of this code by drawing the stack that is formed by the calls to the checkGrid method. In each item in the stack, write the arrayLength passed to the method. Put some sysouts in the code to help you in this simulation.

Best regards,

SCJP 1.4 - SCJP 1.6 - SCWCD in progress
Kalabaw moo
Ranch Hand

Joined: May 26, 2009
Posts: 61
I minimized the problem.

Here's the calling I created. I don't know if this is correct or not, if it's correct then the problem I have is understanding the things that the function will return to be able to see the output. I guess I'm kinda near to the light of understanding the recursion-1 function calling itself twice.

Frank Kellinghusen

Joined: Jul 10, 2009
Posts: 11

I played with your code, so the print statements give a more visual expression. Change the level variable to make deeper recursions.

Which produces following output:
I agree. Here's the link:
subject: recursive method
It's not a secret anymore!