I am trying to add all numbers in an array that are more than the last number it checked. I am getting this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at RaySumLast.sum(RaySumLast.java:16)
at RaySumLast.main(RaySumLast.java:25)
Here is my code:
class RaySumLast
{
static int arr[] = {-99, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5};
static int sum()
{
int sum = 0;
for (int i : arr) {
if (i < arr[i-1])
sum += i;
}
return sum;
}
public static void main(String[] args)
{
cSystem.out.println(sum());
}
}
How can I fix this?