public class TestArray {
public static void main(String[] args) {
int[] ar = {1,2,3,4,5,6,7,8,9};
shiftRight(ar);
for (int i = 0; i <
reverseMethod
receives its own reference to the array and cannot touch the caller's reference.
In other words, it receives the reference by value; if it chooses to store a new value in the local variable which holds it, then that's fine, but it has no effect on other places where references to the old value might be stored.
When you assign the value of temp to ar
ar = temp;
you set the pointer of the method's argument to that value, which does not modify the reference of ar in the main method in any way.
If you want your modifications to "stick", then return the value from that method and assign it in main, like so:
public static void main(String[] args) {
int[] ar = {1,2,3,4,5,6,7,8,9};
ar = reverseArray(ar);
System.out.println();
}
public static int[] reverseArray(int[] ar) {
int[] temp = new int[ar.length];
for (int i = 0, j = temp.length - 1; i < ar.length; i++, j--) {
temp[i] = ar[j];
}
ar = temp;
for (int i = 0; i < ar.length; i++) {
System.out.print(ar[i]);
}
// prints: 876543219
System.out.println();
return ar; // might as well return temp
}
In Java, it's a misnomer to say that objects are passed by reference. It's more accurate to say that the reference to the object is passed by value.
You pass the array reference to reverseArray
by value. The local parameter is a copy of the reference to the array. Later when you say
ar = temp;
You have only pointed the local ar
to temp
, not the original array reference ar
from main
.
On the other hand, in the shiftRight
method, you have directly accessed the array through the copied reference, so the original array's contents change and the method works as expected.
The problem is that you create a local variable temp
array, and you set ar=temp
. You need to actually modify the contents of ar
, rather than creating a new local array and pointing your copied ar
variable to temp
Try something like this.
public static void reverseArray(int[] ar) {
int[] temp = new int[ar.length];
System.arraycopy( ar, 0, temp, 0, ar.length );
for (int i = 0, j = temp.length - 1; i < ar.length; i++, j--) {
ar[i] = temp[j];
}
for (int i = 0; i < ar.length; i++) {
System.out.print(ar[i]);
}
// prints: 876543219
System.out.println();
}