Recursion is used in Java to reverse an array.

Started by Mobo01, December 02, 2022, 01:03:35 AM

Previous topic - Next topic

Mobo01

I'm just getting started with recursion, but I was able to utilize it to construct a basic factorial program without any difficulty. I'm now attempting to construct a recursive function that writes an array in reverse order, but I'm not sure what I'm doing wrong. I'm following the advice in this https://www.scaler.com/topics/reverse-an-array-in-java/">article. I'm not sure what I'm missing. Thank you very much.
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);
  }
}