Quote (05bb94 @ 30 Jan 2019 14:17)
Thank you very much :)
Edit: Would it be possible to solve the task with a recursion ?
Yes, solving it with recursion would require a new function to be defined:
Code
public static int sumRecursive(List<Integer> numbers) {
if (numbers.isEmpty() == true ) {
return 0;
}
else {
return numbers.get(0) + sumRecursive(numbers.subList(1, numbers.size()));
}
}
The function takes one argument (the list of numbers to compute sum for) and adds the first number from the input list to the number obtained from a call to itself, on the remaining items from the input list.
A check is done on the list of numbers to end the recursion, since every recursion step decrements the size of the list by one.
This post was edited by Klexmoo on Jan 30 2019 08:55am