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

I got the IndexOutOfBoundException

You forgot to post the full text of the error message that shows the value of the index and where it happened in the code.

Here is the exception given

 Exception in thread "main" java.lang.IndexOutOfBoundsException: Index:0, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    at java.util.ArrayList.set(arrayList.java:397)
    at sumArrayList.calculateSumArrayList(sumArrayList.java:9)
    at sumArrayList.mail(sumArrayList.java:32)

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index:0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.set(arrayList.java:397)
at sumArrayList.calculateSumArrayList(sumArrayList.java:9)

The error occurred at line 9 where the code tried to access the first element in an empty list.
The error you posted does not come from the code you posted. There is no mail() method (on line 5 of message).

Please post the code and the error the goes with it.

Naming conventions say that variable names should start with a lowercase letter and class names with uppercase. This code is confusing using Integers for a variable instead of a classname.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.