I got the IndexOutOfBoundException in the following code. but I cannot find where went wrong.
import java.util.ArrayList;
public class sumArrayList
{
public static int calculateSumArrayList(ArrayList<Integer> Integers)
{
ArrayList<Integer> duplicate=new ArrayList<Integer>();
for (int i=1;i<=Integers.size();i++)
{
duplicate.set(i,Integers.get(i-1));
}
return calculateSumArrayListHelper(Integers);
}
private static int calculateSumArrayListHelper(ArrayList<Integer> Integers)
{
//first: arrayList size 0
if (Integers.size()==0)
{
return 0;
}
else if (Integers.size()==1)
{
return Integers.get(0);
}
else
{
int last=Integers.remove(Integers.size()-1);
int sum=0;
sum=+last;
}
return calculateSumArrayListHelper(Integers);
}
public static void main(String[] args)
{
ArrayList<Integer> Integers=new ArrayList<Integer>();
Integers.set(0,6);
Integers.add(1,4);
System.out.println(calculateSumArrayList(Integers));
}
}