I am new to Java and i wrote a simple program that calculates the sum of multiples of 3 below 10. I am not sure why i am getting Array Out Of Bounds Exception
int a[] = {},j = 0,sum = 0;
for(int i=1;i<=10;i++)
{
if ((i % 3) == 0)
{
a[j] = i; // Here, i am getting the exception
j++;
}
}
for(int i1=0;i1<j;i1++)
{
sum = sum + a[i1];
}
System.out.println(sum);
}
0values inint a[] = {}, you will get anoutOfBoundsExceptionby trying to set a value of any index in the array. You will need to either set the values of the array or set a size for the array (in which case all positions will be automatically filled with0).