0

public class I5Exc1a {

public static int[] reverse(int[] array)
{
    int[] local = array;
    int i = local.length;
    int j = 0;
    int[] arrayR = new int[i];

    for (;i>-1; i--)
    {
        arrayR[j] = array[i];
        j++;
    }
    return arrayR;
}

}

It is supposed to receive an array and reverse it. but it gives me outofboundexeption when im testing it. I tried fixing it by making an extra array. I think the problem lies in the fact that the length of the array is not passed on correctly to i. Does anyone know how i can solve this?

1 Answers1

0

You are starting from i = local.length and that's already out of bounds.

Look at this array:

[0, 1, 2] - 3 items so length = 3, but if you do array[3] there is no such element, element 2 is at index number 2, starting from 0. Do i-- before your for loop.

Shadov
  • 5,421
  • 2
  • 19
  • 38