0

Hello people of StackOverflow! I have used the searrch option, I've found some related answers but none of them explained why this particular method of reverse ordering of an array in Java doesn't work:

class ReverseOrder
{
    public static void main(String[] args)
    {
        int x[] = {1,2,3,4,5};
        int y[] = x;

        int i, j;
        for(i = 0, j = x.length - 1; i < x.length; i++, j--)
        {
            y[i] = x[j];
        }

        for(int b = 0; b < x.length; b++)
        {
            System.out.println("Inverse order is: " + y[b]);
        }
    }
}

Why is the result 5,4,3,4,5 instead of 5,4,3,2,1??? It drives me absolutely insane and makes no sense to me. Any help would be greatly appreciated!

4 Answers 4

7

Because of this: int y[] = x

y and x are now references to the same array. You should make sure you initialize y as a new array.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, you need to do int y[] = new int[x.length];
Thank you so much! That definitely solved the problem. However I have an extra question - should I do it this way now? int x[] = {1,2,3,4,5}; int y[] = new int[5]; y = x.clone(); Or perhaps there is faster and more efficient way of doing it? Thanks a lot anyway!
it is enough to replace int y[] = x with int y[] = new int [x.lengt], this assures y is initialized and has enough capacity to hold the x content. Y is filled with values in the loop you are filling y in the loop.
2
class ReverseOrder {
    public static void main(String[] args)
    {

        int x[] = {1,2,3,4,5};
        int y[] = new int[5]; // or you could use [x.length]
        int i, j;
        for(i = 0, j = x.length - 1; i < x.length; i++, j--)
        {
            y[i] = x[j];
        }

        for(int b = 0; b < x.length; b++)
        {
            System.out.println("Inverse order is: " + y[b]);
        }
    }
}

2 Comments

well, basically what was explained above, with the x.length ,I did not see the answer before i posted
Thanks a lot man! You people are so quick to answer and help! I wish I could give you all up and mark everyone's answer as accepted! But I just registered :)
1
int y[] = x;

Makes y refer to the same data as x.

y[i] = x[j];

you are also modifying the input array x.

You want y to be totally independent:

int y[] = new int[x.length];

instead.

1 Comment

Oh thank you too! So is that more efficient way than this one: int x[] = {1,2,3,4,5}; int y[] = new int[x.length]; y = x.clone();
1

If you want to reverse the order of array elements in place (that is, modify the array x directly), you can iterate halfway through the array, swapping elements:

final int last = x.length - 1;
final int n_2 = x.length / 2; // round down for odd lengths
for (int i = 0; i < n_2; ++i) {
    int tmp = x[i];
    x[i] = x[last - i];
    x[last - i] = tmp;
}

1 Comment

Thanks a lot!! I've heard of that method, but I was just plain confused and wanted to first clarify the whole case before continuing with studying Java :)

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.