Skip to content

Instantly share code, notes, and snippets.

@efarioli
Created February 10, 2021 15:49
Show Gist options
  • Save efarioli/6d4fd3cdcda825e76805284e9417934c to your computer and use it in GitHub Desktop.
Save efarioli/6d4fd3cdcda825e76805284e9417934c to your computer and use it in GitHub Desktop.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication9;
/**
*
* @author f023507i
*/
public class JavaApplication9
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
int[] mainArr = null;
int[] mainArr2 = {1,2,3,4,5,6};
int[] mainArr3 = {5};
int[] mainArr4 = {};
System.out.println(SumArr(mainArr));
System.out.println(SumArr(mainArr2));
System.out.println(SumArr(mainArr3));
System.out.println(SumArr(mainArr4));
// TODO code application logic here
}
public static int SumArr(int[] arr)
{
if (arr == null || arr.length == 0)
{
return 0;
}
if (arr.length == 1)
{
return arr[0];
}
return SumArrWithSize(arr, arr.length, 0);
}
public static int SumArrWithSize(int[] arr, int size, int accum)
{
if (size == 0)
{
return accum;
}
accum += arr[size - 1];
size--;
return SumArrWithSize(arr, size, accum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment