Recursion sum with generics

Viewed 26

I want to recursively sum all generic elements in an array an the problem I'm facing is during the final return statement I can not use the + operator or any method because its not defined for the type given here is my code:

public static <T extends Number> T binarySum(T[] arr, int low , int high) {
    if(low > high) return null;
    else if(low == high) return arr[low];
    else {
       int mid = (low + high) / 2;
       return binarySum(arr, low, high) + binarySum(arr, mid + 1, high);
    }
}
0 Answers
Related