JAVA Sum of array elements using recursion

preview_player
Показать описание
Simple java code on how to sum array elements using recursion.
I don`t think it`s as fast as it gotta be, but at the moment I haven`t designed any better solution than to use Arrays.CopyOfRange function as function input parameter.
=====================================
=====================================
Рекомендации по теме
Комментарии
Автор

I came up with this for my Java 2 class. Essentially you just give it the array and the starting point you want to add from (usually 0). It doesn't require the CopyOfRange method either.

public static int recurSum(int[] array, int n) {
int sum = array[n];
if (n+1 < array.length) {
sum += recurSum(array, n+1);
}
return sum;
}

quietcode