Home / Data structures and Algorithms by Java Examples / Recursion / Print array using recursion JAVA Example
Print array using recursion JAVA Example
11050 views.
PrintArray.java
class PrintArray {
    //To display array in sequential order.
    public static void printMyArray(int[] data, int index) {
        if (index != -1) {
            printMyArray(data,index - 1);
            System.out.println(data[index]);
        }
    }
    
    //To display array in reverse order.
    public static void printMyArrayReverse(int[] data, int index) {
        if (index != -1) {
            System.out.println(data[index]);
            printMyArray(data,index - 1);
        }
    }
    
    public static void main(String[] args) {
        //data array has five numbers.
        int[] data = {66,33,88,99,100};
        
        //passing data array and last index to start.
        //To print sequential order
        System.out.println("Sequential Order");
        printMyArray(data,data.length - 1);
        
        //To print array in reverse order.
        System.out.println("Reverse Order");
        printMyArrayReverse(data,data.length - 1);
    }
}
Output
Sequential Order
66
33
88
99
100

Reverse Order
100
66
33
88
99
Related Examples
   Simple Recursion Example in JAVA
   Print array using recursion JAVA Example
   Recursion on ArrayList Strings in JAVA Example
   Factorial Program using Recursion in JAVA Example
   Fibonacci Series using Recursion in JAVA Example
   Tree Traversal with Recursion in JAVA Example
   Tree Traversal without Recursion Using Stack Class in JAVA Example
   Is ArrayList Ordered using Recursion in JAVA Example
   Tower of Hanoi using Recursion in Java Example
Copyright © 2016 Learn by Examples, All rights reserved