Recursive Function to Print an Array


public class RecArrayPrint {
    public static void main(String[] args) {

        int[] values = {10,20,30,40,50};

        recArrayPrint(values,5,0);
    }

//  print values in array
//      prints array[position], recursive call starts printing
//      at next position
    static void recArrayPrint(int array[],int size,int position) {
        if (position < size) {
            System.out.print(array[position] + "   ");
            recArrayPrint(array,size,position+1);
        }

//  base case is empty array (size == position), so
//  just print newline
        else
            System.out.println();
    }
}

Output


10   20   30   40   50


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2008