import java.util.*; // Jeremy Lipschutz // Program working through some more recursion methods, introducing the idea // of a public-private pair. The private helper method has parameters that can // keep track of more information. The public method just needs to start off the // recursion public class Recursion2 { public static void main(String[] args) { int[] arr = {5, 2, -15, 10, 2, 7, 9}; System.out.println("The sum of " + Arrays.toString(arr) + " is " + sum(arr)); } // Returns the sum of the contents of the array public static int sum(int[] arr) { return sum(arr, 0); } // Returns the sum of the contents of the array, // starting from the given index private static int sum(int[] arr, int index) { if(index == arr.length) { // finished processing array // base case return 0; } else { // recursive cases // do a little of the work int value = arr[index]; // recurring on a smaller problem return value + sum(arr, index + 1); } } /* public static int sum(int[] arr) { int sum = 0; for(int index = 0; index < arr.length; index++) { sum += arr[index]; } return sum; } */ }