0
public static void reverseArray(int[] arr) {
    int[] reversed = new int[arr.length];
    for (int i = arr.length - 1; i > -1; --i) {
        reversed[arr.length - i - 1] = arr[i];
    }
    arr = reversed;
}

Here I have a reverse array method. If I call

int[] v = new int[] {1, 2, 3, 4};
reverseArray(v);
//print v

I would get "1 2 3 4"

But if I changed the reverseArray function to

public static void reverseArray(int[] arr) {
    int[] reversed = new int[arr.length];
    for (int i = arr.length - 1; i > -1; --i) {
        reversed[arr.length - i - 1] = arr[i];
    }
    for (int i = 0; i < arr.length; ++i) { 
      arr[i] = reversed[i];
    } 
}

It will work as intended and print out "4 3 2 1" Why didn't the "arr = reversed;" work? They are both pointers so if I assign one to the other it should point to the same objects like with the 2D arrays. If I do

int[][] mat = //some assignment

public void reverseMatrix() {
        reverseAllRows();
        int[][] c = new int[mat.length][mat[0].length];
        for (int i = 0; i < mat.length; ++i) {
            c[mat.length - 1 - i] = mat[i];
        }
        mat = c; //This works for some reason
}

It works without having me to copy each one of the elements. Why is that and how do I know when it is the case to use equals and when not to?

  • Your problem is that Java is doing "pass by value". This means that the assignment "arr = reversed" simply has no effect outside of the scope of that method. The code that invoked the reverse method does **not** see the a new, reversed array. You ever have to reverse the elements of the provided array directly; or you have to return the reversed array. – GhostCat Mar 17 '16 at 23:42
  • Yeah but why did it work for the 2D array? – user2180617 Mar 17 '16 at 23:44
  • 1
    Because for the 2D array, you are doing something different. There your method does not work on an input parameter, but on a field of the enclosing class! – GhostCat Mar 17 '16 at 23:46
  • Ohh I never knew that. So it is ok to use "=" assignment if variable is within the class? – user2180617 Mar 17 '16 at 23:52
  • That depends on what you want to do. I suggest you turn back to learning mode: these are fundamental things. You should not even think about arrays, when you don't understand the difference between a field of a class and the parameters of methods. – GhostCat Mar 17 '16 at 23:53
  • The universal rule in Java is that you cannot change what references passed into your method refer to, but you can change the contents of the object that reference refers to. That is true for all objects, including arrays. – Louis Wasserman Mar 18 '16 at 00:15

2 Answers2

1

What you will have to do is return the array as such:

public static int[] reverseArray(int[] arr) 
{
    int[] reversed = new int[arr.length];
    for (int i = arr.length - 1; i > -1; --i) 
    {
        reversed[arr.length - i - 1] = arr[i];
    }
    return reversed;
}

Then you will call the function:

int[] v = new int[] {1, 2, 3, 4};
v = reverseArray(v);
//print v
Xzenon
  • 1,193
  • 2
  • 15
  • 37
0

Java is a Pass-By-Value language. That is the reason. When you pass a reference, and assign that reference to some other object, the original reference of the caller will not change. Thus changes won't be visible to caller.

Ferit
  • 8,692
  • 8
  • 34
  • 59