-4
public static int[] organizaC(int[] v) {
    int i, temp = 0;
    if (v.length > 0) {
        for (i = 0; i < v.length; i++) {
            if (v[i] > v[i + 1]) {
                temp = v[i];
                v[i] = v[i + 1];
                v[i + 1] = temp;
                i = 0;
            }
        }
        return v;
    } else {
        return null;
    }
}

I'm getting an ArrayOutofBoundsIndex exception when trying to use this function but the IDE won't let me use the debugger. Anyone knows what is happening? Am I causing some buggy loop?

Stav Alfi
  • 13,139
  • 23
  • 99
  • 171

1 Answers1

3

Imagine what happens when i equals v.length - 1, your if statment :

if(v[v.length-1] > v[v.length-1+1])

you try to acces your array at index v.lenght but as arrays' indices go from 0 to v.length-1, you are out of bounds!

azro
  • 53,056
  • 7
  • 34
  • 70
tom gautot
  • 378
  • 2
  • 10