I'm just getting started with recursion, and while studying this article, I was able to utilise it to construct a basic factorial programme 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. What am I overlooking? 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);
}
}
Solved! Go to Solution.
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, 9);
}
public static void reverseDisplay(int[] ary, int position){
if(position > 0) {
System.out.print(ary[position]);
reverseDisplay(ary, position - 1);
}
}
}
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, 9);
}
public static void reverseDisplay(int[] ary, int position){
if(position > 0) {
System.out.print(ary[position]);
reverseDisplay(ary, position - 1);
}
}
}