4

I want my method to sum a list of integers using recursion and return that list.

Here is my attempt:

    public static int sumListRecursive(List<Integer> numbers) {
        if (numbers.isEmpty() == true ) {
            return 0;
        }
        else {
            System.out.println(numbers.get(0) + sumListRecursive(numbers.subList(1, numbers.size())));
            return numbers.get(0) + sumListRecursive(numbers.subList(1, numbers.size()));
        }
    }

And in my main method I have this:

        List<Integer> numbers = new ArrayList<Integer>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(4);
        sumListRecursive(numbers);

And when i run the program I get this in console: 10 4 7 4 9 4 7 4

So what's wrong?

6
  • 1
    I see nothing wrong with your code. The above code does not give you that console output. Where are you printing it in the console? Commented Nov 23, 2015 at 12:30
  • remove the System.out.println(); from recursive function. Place it in your method call from main. Commented Nov 23, 2015 at 12:32
  • Hello @Raf, I forgot that I've a line of code "System.out.println(numbers.get(0) + sumListRecursive(numbers.subList(1, numbers.size())));" above the return word in the else block. Commented Nov 23, 2015 at 12:32
  • It works now, the console just prints 10! Much thanks! :) Commented Nov 23, 2015 at 12:33
  • small comment on condition check "if (numbers.isEmpty() == true)" is same "if (numbers.isEmpty())", it makes little simple. Commented Nov 23, 2015 at 12:36

1 Answer 1

3

I have provided the explanation in the code in the form of comment. He has to print the sum in the main after recursive function finished. Putting the print statement in the recursive function causes a value to be printed in the console every time.

See the correction below:

 public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<Integer>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(4);
        //print the sum in here 
        System.out.println(sumListRecursive(numbers));


    }

    public static int sumListRecursive(List<Integer> numbers) {
        if (numbers.isEmpty() == true ) {
            return 0;
        }
        else {
            /* removed the print statement from here as it prints each time the function is called and else is executed. */
            return numbers.get(0) + sumListRecursive(numbers.subList(1, numbers.size()));
        }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

A little explaination does not hurt. :)
Can you explain why this is an answer? (I've seen your above comments but it should be self contained)
@YassinHajaj initially I did put a comment in the answer itself and thought that was sufficient. I have now updated the answer to include further explanation.
@Hauru you are welcome, don't forge to mark the question as accepted and if you find it helpful vote up please. cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.