Hello there I'm new to programming and I'm currently tasked to create a program relating to Sorting. and I keep getting the error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at sorting.Sorting.main(Sorting.java:17)
Java Result: 1
The line it tells where the error is line 17 which contains:
if (ArrayOfInts[j] > ArrayOfInts[j + 1])
please help.. heres the whole thing.
public class Sorting{
public static void main(String[] args){
int[] ArrayOfInts = {42, 97, 3, 689, 14, 1076, 3000, 8, 632, 327, 976, 4215};
for(int i = ArrayOfInts.length; i >= 1; i--){
for(int j = 0; j < i; j--){
if (ArrayOfInts[j] > ArrayOfInts[j + 1]){
int temp = ArrayOfInts[j];
ArrayOfInts[j] = ArrayOfInts[j + 1];
ArrayOfInts[j - 1] = temp;
}
}
}
for(int i = 0; i < ArrayOfInts.length; i++){
System.out.println(ArrayOfInts[i] + " ");
}
}
}
When
j=0
, this line is problematic:as you try to access -1 index in an array.