0

I am just starting to learn recursion and was able to use it write a simple factorial program without much of a problem. Now I am trying to write a recursive method that writes an array in reverse order but I can't figure out what I'm doing wrong. What am I missing? Thank you.

import java.io.*;

public class Recursion {
  public static void main(String[] args) throws IOException{
    int myArray[] = {1,2,3,4,5,6,7,8,9,10};
  }

  public static void reverseDisplay(int[] ary, int position){
    if(position > 0)
      System.out.print(ary[position]);
     reverseDisplay(ary, position - 1);
  }
}
6
  • Please clarify exactly what is going wrong, preferably with some examples. Commented Feb 12, 2016 at 17:47
  • Maybe System.out.print(ary[position-1]); ? The ending condition if(position > 0) seems to suggest that. Commented Feb 12, 2016 at 17:47
  • When I run the code I have no output at all. Commented Feb 12, 2016 at 17:48
  • Where is your call to reverseDisplay in main? call it ! Commented Feb 12, 2016 at 17:49
  • Well, you never actually call reverseDisplay, so that would be why. Could you try testing the function itself? Commented Feb 12, 2016 at 17:50

3 Answers 3

4
  • You do not call your recursion method.

  • You have an endless recursion because reverseDisplay() is always being executed due to missing enclosing brackets.

  • Also your stop condition has to be >= 0 because the first index of an array is 0.

Your method should read:

import java.io.IOException;

public class Recursion {
    public static void main(String[] args) throws IOException{
        int myArray[] = {1,2,3,4,5,6,7,8,9,10};

        reverseDisplay(myArray, myArray.length -1);
    }

    public static void reverseDisplay(int[] ary, int position){
        if(position >= 0) {
            System.out.print(ary[position]);
            reverseDisplay(ary, position - 1);
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I understand what I did wrong now.
0

When doing recursion you need something called a base case. You need the base case to end the recursion or you will get a stack overflow.

You might try something like this. The return statement stops the recursion from being endless.

public static void reverseDisplay(int[] ary, int position){
     if(int == -1)
         return;
     System.out.print(ary[position]);
     reverseDisplay(ary, position - 1);
  }

Comments

0

Try:

import java.io.*;

public class Recursion {
  public static void main(String[] args) throws IOException{
    int myArray[] = {1,2,3,4,5,6,7,8,9,10};

    reverseDisplay(myArray,0);
  }

  public static void reverseDisplay(int[] ary, int position){
    if(position == ary.length){
        return;
    }

    reverseDisplay(ary, position + 1);
    System.out.print(ary[position]);
  }
}

If position == ary.length you finish the recursion, else you call reverseDisplay recursively and after that print the current position of ary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.