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.

4
  • rorder[rorder.length] is illegal. Arrays are zero-based. Commented Nov 26, 2013 at 19:13
  • 1
    use int j = rorder.length - 1; Commented Nov 26, 2013 at 19:13
  • Thank you Nandkumar thats fixed my problem Commented Nov 26, 2013 at 19:15
  • you do realize you are reversing an empty array, right ? Commented Nov 26, 2013 at 19:33

7 Answers 7

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]
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

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).

Comments

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]

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.