import java.util.Arrays;

/**
 * A program to print arrays in reverse order using recursion.
 *
 * @author Mark Young (A00000000)
 */
public class ReverseArray {

    public static void main(String[] args) {
        // create two sample arrays
        int[] arr1 = new int[]{6, 100, 3, -2, 8, 18, 5};
        int[] arr2 = new int[]{42, 17, -5, 86, 99};

        // show the first one in reverse order
        System.out.println();
        System.out.println(Arrays.toString(arr1) + " in reverse: ");
        printInReverse(arr1, arr1.length);
        System.out.println();

        // show the second one in reverse order
        System.out.println();
        System.out.println(Arrays.toString(arr2) + " in reverse: ");
        printInReverse(arr2, arr2.length);
        System.out.println();
    }

    /**
     * Print the front part of an array in reverse order.
     * Requires to be told how much of the array to print.
     * (Uses recursion.)
     *
     * @pre  0 <= len < arr.length
     * @post the elements arr[0..len-1] have been printed in reverse order.
     *
     * @param arr the array to be printed in reverse order
     * @param len the part of the array to be printed (must be at front)
     */
    public static void printInReverse(int[] arr, int len) {
        if (len > 0) {
            // first print out the last element...
            System.out.println("Print \t" + arr[len - 1] + "\t and reverse "
                    + Arrays.toString(Arrays.copyOf(arr, len - 1)));
            // ... then print out the rest
            printInReverse(arr, len - 1);
        } else {
            System.out.println("(done!)");
        }
    }

}
