0
public static void reverse(int[] arr)
    {
        int[] a= arr;
        int index=a.length-1;
        for (int i=0; i<a.length; i++){
            System.out.println(ArrayUtil.toString(a));
            if (index>=0){
                arr[index]=a[i];
                index--;
            }
        }
    }

So, I'm trying to reverse the order of the elements in a given array. I declare another temporary array "a" that's the same as the given array. However, when going through the loop to reverse the order or elements, somehow the array "a" is also being changed. I don't see how this is possible. int[] arr in this case was [14,2,19,3,15,22,18,7] and the end result is [14,2,19,3,3,19,2,14] for both []arr and []a. Help would be really appreciated, thank you.

Also, I'm new to this forum, so I'm sorry if I didn't follow proper format

THANK YOU VERY MUCH for everyone who contributed! Amazing and fast responses!

3
  • 1
    "somehow the array "a" is also being changed." What do you think int[] a= arr; does ? Commented Nov 29, 2014 at 21:23
  • 2
    It is the same array. You set it so when you declare a. If you want the same length new array, do new int[arr.length]. Commented Nov 29, 2014 at 21:24
  • This is called Pointer aliasing, (en.wikipedia.org/wiki/Pointer_aliasing), it's caused because both a and arr point to the same memory location. So when one changes so does the other. Commented Nov 29, 2014 at 21:26

4 Answers 4

2

Use:

int[] a = new int[arr.length]
System.arraycopy( arr, 0, a, 0, arr.length);

to copy the actual elements inside and not the reference of the array. (Copying the reference means that both a and arr will refer to the same object, so any change to a is also a change to arr. This happens for all objects, as opposed to just primitives that are copied by value)

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

Comments

1

Change algorithm for this. Even if you will fix your code, to make it working, you will use additional memory for it. Better use:

for (int i = 0; i < a.length / 2; i++){
            int tmp = a[i];
            a[i] = a[a.length - i - 1];
            a[a.length - i - 1] = tmp;
        }

Comments

0

Any modifications to arr will change a, and same thing vice versa, because they are references to the same array. You need to completely copy the array if you want two independent arrays:

int[] a = new int[arr.length];
for (int i = 0; i < a.length; i++) a[i] = arr[i];

Or using System.arrayCopy:

int[] a = new int[arr.length];
System.arraycopy(arr, 0, a, 0, a.length);

Comments

0

Actually with

int[] a= arr;

the same array's reference is given to array a. And when you reversely assign values to arr, these values are actually being placed from the end of a in reverse order. So at the end you get symmetric array

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.