I am getting an Array Index out of Bounds error, however I am unable to see how I am aceesing an out of Index position. Here is my code:

public class Solution {

public ArrayList sieve(int a) {

int i,j;

// ArrayList res=new ArrayList<>();

ArrayListPrimes=new ArrayList<>(a+1);

for(i=0;i<=a;i++)

{

Primes.set(i,1);

}

Primes.set(0,0);

Primes.set(1,0);

for(i=2;i<=Math.sqrt(a);i++)

{

if(Primes.get(i)==1)

for(j=2*i;j<=a;j+=i)

{

Primes.set(j,0);

}

}

return Primes;

}

}

Thanks in advance.