Introduction: Recursively Summing an Array in Java

Recursion is a very useful and time efficient procedure that can quickly solve a problem with very little code. Recursion involves the method you create calling itself shortening the original problem.

For this example, we will be summing an array of 10 integers, but the size could be of any length.

Supplies

You should know basic java syntax and have your IDE or a text editor to write your code for this task.

Step 1: Set Up Your Main Method

To begin, set up your main method in a newly created class. I've named my class RecursiveSum. This is where you will create the array of integers and call your recursive method.

Step 2: Create Your Recursive Method Header

Outside your main method, create the method header for your recursive method.

The method is static, as it will not require an object to use it on.

The return type is int, as the array we will be using will be full on integers. However, this can be changed to whatever number type the array contains.

I've named my method recursiveSum which will take two parameters; an array of integers and the index we will be adding to the sum. I've called these parameters numbers and index respectively.

You will see errors right now and that is fine. They will be fixed later on.

Step 3: Create Your Kicker/base Case

A recursive method needs a kicker/base case. This is the condition that will stop your method from infinitely calling itself. This base case can be thought as the most simple case we will encounter. In this case, the base case will be when we are at the end of our array. If the current index equals the length of the array (minus 1 because arrays start counting from 0 not 1), we are at the end and we simply return that element at that index.

Step 4: The Recursive Step

Once we have our base case, the next step is our recursive step. This is where the magic happens. We've handled the case when our index equals the last element in our array. What if we aren't at the last element in our array? What if we could simply tell it to add our current element plus the next? Eventually we will hit the end of our array and our base case will take affect.

To accomplish this, we simply return our current index and "add the rest" of the array.

Step 5: Shorten the Problem

How do we simply "add the rest"? We already have a method that will add a certain element; our recursiveSum() method! We can call it again but change which index we are summing.

We pass in the same array we are processing, but we pass in the next index from our current index. We do this by simply adding one to our current index as shown.

Step 6: Create the Array of Integers

Now that our recursive summing method is complete, we can create our array that we will process. This array will be in our main method block.

You can make the size of the array as long as you'd like. I've created a few different arrays with different sizes and values to show it works on not just a single size.

Step 7: Call the Method With Your Arrays

Now you can call your recursive method and pass these arrays to it. Now you can run your program.

Step 8: Print the Results

Nothing happened. Why? Recursive sum returns an integer but we haven't done anything with this integer. It did its job but we can't see the result. To see the result, we simply print it out like so. After running this you should see the results for each of your arrays.

Step 9: Congratulations

You've completed a recursive function. Feel free to change the size of your arrays. If you test it out, you'll notice it crashes when you have an empty array. We haven't accounted for it but that's a great way to improve your recursive method.