recursively sum the integers in an array

2019-01-23 19:08发布

I have a program that I'm trying to make for class that returns the sum of all the integers in an array using recursion. Here is my program thus far:

public class SumOfArray {

private int[] a;
private int n;
private int result;

    public int sumOfArray(int[] a) {

      this.a = a;
      n = a.length;

      if (n == 0)  // base case
      result = 0;
      else
          result = a[n] + sumOfArray(a[n-1]);

      return result;

   } // End SumOfArray method

} // End SumOfArray Class 

But I'm getting three error which are all related, I believe, but I can't figure out why it is finding a type of null:

SumOfArray.java:25: sumOfArray(int[]) in SumOfArray cannot be applied to (int)
    result = a[n] + sumOfArray(a[n-1]);
                    ^
SumOfArray.java:25: operator + cannot be applied to int,sumOfArray
    result = a[n] + sumOfArray(a[n-1]);
              ^
SumOfArray.java:25: incompatible types
found   : <nulltype>
required: int
    result = a[n] + sumOfArray(a[n-1]);
                  ^
3 errors

8条回答
来,给爷笑一个
2楼-- · 2019-01-23 19:47

The issue is that a[n-1] is an int, whereas sumOfArray expects an array of int.

Hint: you can simplify things by making sumOfArray take the array and the starting (or ending) index.

查看更多
虎瘦雄心在
3楼-- · 2019-01-23 19:51
private static int sum(int[] arr) {
    // TODO Auto-generated method stub
    int n = arr.length;

    if(n==1)
    {
        return arr[n-1];
    }

    int ans = arr[0]+sum(Arrays.copyOf(arr, n-1));

    return ans;
}
查看更多
放我归山
4楼-- · 2019-01-23 19:56

a is an int array. Thus a[n-1] is an int. You are passing an int to sumOfArray which expects an array and not an int.

查看更多
地球回转人心会变
5楼-- · 2019-01-23 19:59

Try this if you don't want to pass the length of the array :

private static int sumOfArray(int[] array) {

        if (1 == array.length) {
            return array[array.length - 1];
        }

        return array[0] + sumOfArray(Arrays.copyOfRange(array, 1, array.length));
    }

Offcourse you need to check if the array is empty or not.

查看更多
淡お忘
6楼-- · 2019-01-23 20:01

This is the one recursive solution with complexity O(N).and with input parameter A[] only.
You can handle null and empty(0 length) case specifically as Its returning 0 in this solution. You throw Exception as well in this case.


/*
 * Complexity is O(N)
 */
public int recursiveSum2(int A[])
{
    if(A == null || A.length==0)
    {
        return 0;
    }
    else
    {
        return recursiveSum2Internal(A,A.length-1);
    }
}
private int recursiveSum2Internal(int A[],int length)
{
    if(length ==0 )
    {
        return A[length];
    }
    else
    {
        return A[length]+recursiveSum2Internal(A, length-1);
    }
}
查看更多
手持菜刀,她持情操
7楼-- · 2019-01-23 20:01

How about this recursive solution? You make a smaller sub-array which contains elements from the second to the end. This recursion continues until the array size becomes 1.

import java.util.Arrays;

public class Sum {
    public static void main(String[] args){
        int[] arr = {1,2,3,4,5};
        System.out.println(sum(arr)); // 15
    }

    public static int sum(int[] array){
        if(array.length == 1){
            return array[0];
        }

        int[] subArr = Arrays.copyOfRange(array, 1, array.length);
        return array[0] + sum(subArr);
    }
}
查看更多
登录 后发表回答