0

I have a problem with creating reverse ordered array. Here is the code that I wrote:

public static int[] makeReverse(int number) {
    int[] rorder = new int[number];
    int j = rorder.length;

    for (int i = 0; i < rorder.length-1; i++) {

        rorder[j] = i;
        j--;
    }

    return rorder;  

But when I try to run it I got java.lang.ArrayIndexOutOfBoundsException error. I couldn't find the error.

Zong
  • 6,160
  • 5
  • 32
  • 46
JayGatsby
  • 1,541
  • 6
  • 21
  • 41

7 Answers7

2

You can initialized j to rorder.length, and accessing rorder[j]. That is clearly not possible. You can't access the index arr.length. Changing j to rorder.length - 1 will solve the exception.

Even though the exception gets resolved, what you are doing is not reversing the array. You are just filling your array from number rorder.length - 2 to 0 from 1st index to 2nd last (I said 2nd last, b'coz your loop is iterating only till 2nd last index).

What you should do is, iterate till half the length of the array, and swap the element from the beginning with the elements at the end:

for (int i = 0; i < arr.length / 2; ++i) {
    // swap arr[i] with arr[arr.length - i - 1]
}
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
1

Quoting this post you can use ArrayUtils for this:

With Commons.Lang, you could simply use

ArrayUtils.reverse(int[] array)

Most of the time, it's quicker and more bug-safe to stick with easily available libraries already unit-tested and user-tested when they take care of your problem.

Community
  • 1
  • 1
1

Try this, pass your number as input parameter

int[] reverse(int input){
    int[] reverse = new int[input];
    int x = 0;
    for(int i = input; i>0; i--){
         reverse[x] = i;
         x++;
    }
    return reverse;
}

It will generate an array of int from input to 1

Shamse Alam
  • 1,285
  • 10
  • 21
0

In your approach, you have to change the line:

int j = rorder.length;

to

int j = rorder.length-1;

Because rorder.length will return the size of the array, but the maximum array index is 1 less than size of the array(as the array index starts from 0).

Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
0

Say this method is called with makeReverse(5) you will create rorder of length 5.

With indexes 0,1,2,3,4 (remember arrays start at 0) then you make j = rorder.length (with is the same as number which is 5).

Then, in the first line of your for loop you call rorder[j] or rorder[5] which does not exist.

Shashi
  • 12,487
  • 17
  • 65
  • 111
Joe
  • 800
  • 4
  • 10
  • 26
0

You can do this way:

public static void main(final String[] args) {
    final int[] rorder = makeReverse(6);
    System.out.println(Arrays.toString(rorder));
}

public static int[] makeReverse(final int number) {
    final int[] rorder = new int[number];
    for (int i = number; --i > 0;) {
        rorder[i % number] = i;
    }
    return rorder;
}

Output:

[0, 1, 2, 3, 4, 5]
Luca Mastrostefano
  • 3,201
  • 2
  • 27
  • 34
0

Change

int j = rorder.length;

for (int i = 0; i < rorder.length-1; i++) {

to

int j = rorder.length - 1; //Comment 1

for (int i = 0; i < rorder.length; i++) { //comment 2
  1. Otherwise on the first iteration will be an assignment to j[4] - the max is j[3].

  2. The for loop is doing 1 iteration too few in your code e.g if "number" is 5, "j" will be decremented to 0 but then the loop will exit, so rorder[0] won't be assigned the value 4.

splrs
  • 2,424
  • 2
  • 19
  • 29