Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用java语言递归查找数组中所有元素的和_Java_Arrays_Recursion_Sum - Fatal编程技术网

用java语言递归查找数组中所有元素的和

用java语言递归查找数组中所有元素的和,java,arrays,recursion,sum,Java,Arrays,Recursion,Sum,这是我的密码: 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[inde

这是我的密码:

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);
}
public static int sumArrayRecursion(int array[], int n){
    if (n == 0){
        return 0;
    }
    else {
        return array[n-1] + sumArrayRecursion(array, n-1);
    }
}   

我不断出现越界错误,但我不知道我做错了什么。

您的基本条件有问题。应该是:

if (index == array.length)
注意,第一次调用时需要传递index=0。如果要传递index=array.length-1,则保持基本大小写不变,并将递归方法调用更改为传递index-1,而不是index+1

然而,您真的需要递归吗?我会认真思考数百次,然后再尝试递归而不是循环来完成这项任务。

试试

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);
    }
}

@Masud-Your's代码有一个逻辑错误,尽管我是Java初学者,如果我不正确,非常抱歉

return array[index] + sum(array, index - 1);

     array[index]    
当索引从0开始时,将收到一个越界错误-因此意味着那里不会有索引。 “索引-1”将起作用。 此外,这会将基本大小写更改为返回“0”,因为返回的数组[0]将添加两次数组[0],且总和不正确

这是我的代码:

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);
}
public static int sumArrayRecursion(int array[], int n){
    if (n == 0){
        return 0;
    }
    else {
        return array[n-1] + sumArrayRecursion(array, n-1);
    }
}   

不是从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);
}

如果您使用的是Java1.8,那么可以执行以下操作

public int sum(int[] array) 
{
     return (int)array.stream().sum();
}
甚至

public int sum(int[] array) 
{
     return (int)array.sum();
}

你的停车条件在哪里?如果index是数组中的元素数,sum{9},1将使index超出范围:array[1]+sumarray,0。数组中只有一个元素,数组[1]不存在。请看我的main方法。这应该是sum{9},0数组是空的呢。你有一个负指数。