-2

What does each line of the code below do? I am very new to Java, and I am having difficulty understanding recursion.

`public class RecursionExamples {
    static Scanner in = new Scanner(System.in);
    public static void main(String[] args) {
    //use recursion to print a list in reverse order
    int[] numList = {
        10,
        20,
        30,
        40,
        50
    };
    reversePrint(numList);
    System.out.println("");
}
private static void reversePrint(int[] numbers) {
    if (numbers.length == 0)
        return;
    int[] a = new int[numbers.length - 1];
    for (int i = 0; i < numbers.length - 1; i++)
        a[i] = numbers[i + 1];
    reversePrint(a);
    System.out.println(numbers[0] + "");


   }
}`  
M.B
  • 49
  • 6
  • 3
    I found it useful to step through recursive code with a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems?noredirect=1&lq=1) to get an idea. If the issue is the recursion, I'd try to wrap my head around it with a less obfuscated example, like factorial or the recursive [sum of numbers (1...n)](https://stackoverflow.com/questions/37628457/java-recursion-sum-of-number-and-how-it-work/37628492) – Curiosa Globunznik Dec 12 '19 at 03:48
  • Desk checking, does no one do desk checking anymore? – MadProgrammer Dec 12 '19 at 04:00
  • @MadProgrammer I do, I do – Scary Wombat Dec 12 '19 at 04:03
  • @MadProgrammer I do, I do too, but here didactic factors prevail – Curiosa Globunznik Dec 12 '19 at 04:06

2 Answers2

0

In your code, print statement in reversePrint should be like this,

System.out.println(numbers[numbers.length - 1] + ""); // for printing from left -> right

Recursion can be confusing to many of the people. Let me try my best to explain.

As you already may know, A Recursive programming contains basically a method which calls repeatedly itself until a certain condition is met. Note this point,

Until a certain condition is met

. This is very important in stopping the continuous execution of the method, which may lead to StackOverflow exception.

Okay coming to the point, in a recursive method, the first thing you need to do is to check if to continue execution or to return the control statement.

For simplicity, let us take only 10,20 as input for your example code.

  1. First, main method calls the recursive(recursivePrint) method for printing the elements.
  2. Inside reversePrint method, as I said before, you are checking if to continue or to return the control by checking the number of elements in the array. This is because if there is no elements in array, then there is no point in continuing the program.
  3. Next, you are preparing the next set of array to pass to the reversePrint method by decrementing the array size(Current array size - 1). In 2nd point, you had 10,20 as two elements in array. Now, you are constructing new array with 10 as element(the idea here is to seperate individual elements from right to left, so that you can print it from right to left) as pass it to reversePrint method. Thing to remember : Still some code is left behind to print the last element(which is 20) which will be executed once this recursive method call is finished.
  4. Check if the array is empty, since we have 10 as the element, construct new array with of zero elements or empty array (Current array size(which is ONE) - 1 = 0) and call the recursivePrint method and pass this empty array. Thing to remember : Still some code is left behind to print the last element(which is 10) which will be executed once this recursive method call is finished.
  5. Again, check if array is empty, since it is empty now, simply return to the control statement.
    1. Now the control goes back to execute the code which remained in previous step i.e continuous to print the element 10 in 4th point.
    2. Since, the control statement reached the end of method, it goes back to print the element 20 as said in the 3rd Point.
    3. Again it reaches the end of method, and control statement returns back to the main method and prints empty string with no content.

Hope this helps you a little bit in understanding. All the best. Once you understand, it will be fun to work with it.

Unknown
  • 531
  • 5
  • 18
0

Here is an accounting of what your program is doing:

  • The main method declares an array of ints and passes a reference to that array to reversePrint. This is the first time calling reversePrint, so we will call it call #1.
  • reversePrint (call #1) receives a reference to an array named numbers that contains [10, 20, 30, 40, 50].
    • numbers.length is not 0, so the method continues.
    • A new array named a is created with one element shorter than numbers. It is populated with [20, 30, 40, 50].
    • While still in reversePrint #1, we call reversePrint (which will be call #2) with a reference to a that contains [20, 30, 40, 50]. But our reversePrint #1 isn't done yet. It is waiting for the result of call #2.
  • reversePrint (call #2) receives a reference to an array named numbers that contains [20, 30, 40, 50].
    • numbers.length is not 0, so the method continues.
    • A new array named a is created with one element shorter than numbers. It is populated with [30, 40, 50].
    • While still in reversePrint #2, we call reversePrint (which will be call #3) with a reference to a that contains [30, 40, 50]. But our reversePrint #2 isn't done yet. It is waiting for the result of call #3.
  • reversePrint (call #3) receives a reference to an array named numbers that contains [30, 40, 50].
    • numbers.length is not 0, so the method continues.
    • A new array named a is created with one element shorter than numbers. It is populated with [40, 50].
    • While still in reversePrint #3, we call reversePrint (which will be call #4) with a reference to a that contains [40, 50]. But our reversePrint #3 isn't done yet. It is waiting for the result of call #4.
  • reversePrint (call #4) receives a reference to an array named numbers that contains [40, 50].
    • numbers.length is not 0, so the method continues.
    • A new array named a is created with one element shorter than numbers. It is populated with [50].
    • While still in reversePrint #4, we call reversePrint (which will be call #5) with a reference to a that contains [50]. But our reversePrint #4 isn't done yet. It is waiting for the result of call #5.
  • reversePrint (call #5) receives a reference to an array named numbers that contains [50].
    • numbers.length is not 0, so the method continues.
    • A new array named a is created with one element shorter than numbers. It is populated with [].
    • While still in reversePrint #5, we call reversePrint (which will be call #6) with a reference to a that contains []. But our reversePrint #5 isn't done yet. It is waiting for the result of call #6.
  • reversePrint (call #6) receives a reference to an array named numbers that contains [].
    • numbers.length is 0, so the method returns to the caller, #5.
  • reversePrint #5 completes, printing out the first element of its array, 50.
  • reversePrint #4 completes, printing out the first element of its array, 40.
  • reversePrint #3 completes, printing out the first element of its array, 30.
  • reversePrint #2 completes, printing out the first element of its array, 20.
  • reversePrint #1 completes, printing out the first element of its array, 10.
  • main completes, printing out a blank line.
Mitz
  • 1