I have given Dave's posts some "thumbs up" to indicate that you might want to pay attention to his posts the most.
Tim Driven Development
posted 1 year ago
For each call to that recursive method, if you pass a positive integer, it calls the method for one less (so passing 2 causes the method to be invoked later with 1). Then it prints the nth element of the array, remembering that in order to find the nth element of an array, you pass n − 1. So to find the second element when the argument is 2, it uses index 1.
Once you get below the first element of the array, the test for i==0 will stop the execution.
Because you call the method with n − 1 before you print array[n], you will get the array elements printed in their normal order.
After line 10, which is inside the else, line 11 is outside any particular control structures.
Think that way, beware: I got some weird imagination today.
Your scenario looks like that now. Imagine there is a cave. Outside the cave there are (9) fireworks on the ground, but you can't shoot them unless you go to the cave and then come back. So, when you do a recursive call, you go to the cave. Now that you are deeper in a cave, there you can also see fireworks on the ground, but 1 less this time (8), but you can't shoot them too unless you go deeper to the cave and then come back... imagine story continues till there is only 1 firework on the ground, and yet again you decide go deeper again, you go there, and you find out that there is nowhere else to go, so you have to come back. Now that you coming back, you were told you'll be able to shoot fireworks on the way back, so you start shooting them. So you shoot 1 (the ones you saw lately), then you go back further way out and you shoot 2, then 3, then 4, ... 9 which were apparently outside the cave. And you stop, end of printArray() body.
Please don't tell you didn't like my story
posted 1 year ago
Perfect story to understand recursive logic... for newbies !!