Recursion is used in Java to reverse an array.

For the Compleat Fan
Locked
Mobo01
Posts: 8
Joined: Thu Jun 23, 2022 8:57 am

Recursion is used in Java to reverse an array.

Post by 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 article. I'm not sure what I'm missing. Thank you very much.

Code: Select all

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);
  }
}

Locked