java语言递归求数组中所有元素的总和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20151149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find the sum of all elements in array recursively in java language
提问by
Here is my code:
这是我的代码:
public int sum(int[] array, int index)
{
//int index is the number of elements in the array.
//Here is my base case:
if (index == 0)
return 0;
//Now it's time for the recursion
else
return array[index] + sum(array, index + 1);
}
I keep on getting an out of bounds error, but I don't what I am doing wrong.
我不断收到越界错误,但我不知道我做错了什么。
回答by Rohit Jain
Your base condition is faulty. It should be:
你的基本条件有问题。它应该是:
if (index == array.length)
Note, you need to pass index = 0
on first call. If you are passing index = array.length - 1
, then keep the base case as it is, and change the recursive method invocation to pass index - 1
, instead of index + 1
.
请注意,您需要传递index = 0
第一个电话。如果您正在传递index = array.length - 1
,则保持基本情况不变,并将递归方法调用更改为 pass index - 1
,而不是index + 1
。
However, do you really need recursion? I would seriously give it hundreds of thoughts before reaching out for recursion instead of loop for this task.
然而,你真的需要递归吗?在针对此任务进行递归而不是循环之前,我会认真地给它数百个想法。
回答by Masudul
Try,
尝试,
public static void main(String[] args){
int arr[] = {3, 4, 6, 7};
System.out.println(sum(arr, arr.length-1));
}
public static int sum(int[] array, int index) {
if (index == 0) {
return array[0];
} else {
return array[index] + sum(array, index - 1);
}
}
回答by Timothy Do
@ Masud - you're code has a logical error though (i'm beginner Java so sorry if i'm incorrect).
@ Masud - 不过你的代码有一个逻辑错误(我是 Java 初学者,如果我不正确,很抱歉)。
return array[index] + sum(array, index - 1);
array[index]
will receive a out-of-bounds error as index starts at 0 - hence meaning there won't be an index there. 'index - 1' would work. Also, this would change your base case to return '0' as returning array[0] will have array[0] added twice and an incorrect sum.
将收到越界错误,因为索引从 0 开始 - 因此意味着那里不会有索引。'index - 1' 会起作用。此外,这会将您的基本情况更改为返回“0”,因为返回 array[0] 会将 array[0] 添加两次并且总和不正确。
This is my code:
这是我的代码:
public static int sumArrayRecursion(int array[], int n){
if (n == 0){
return 0;
}
else {
return array[n-1] + sumArrayRecursion(array, n-1);
}
}
回答by Sumama Waheed
instead of going from 0 to highest index , go from highest index to 0, i did (index -1) because you said index is total elements, so if array has 10 elements, last element has index 9
而不是从 0 到最高索引,从最高索引到 0,我做了(索引 -1),因为你说索引是总元素,所以如果数组有 10 个元素,最后一个元素的索引为 9
public int sum(int[] array, int index)
{
//int index is the number of elements in the array.
//Here is my base case:
if (index == 0)
return 0;
//Now it's time for the recursion
else
return array[index-1] + sum(array, (index - 1);
}
回答by Theresa Forster
If you are using Java 1.8 you can do the following
如果您使用的是 Java 1.8,您可以执行以下操作
public int sum(int[] array)
{
return (int)array.stream().sum();
}
or even
甚至
public int sum(int[] array)
{
return (int)array.sum();
}