I was playing with arrays and manipulating them in different ways, and I stumbled across some very strange behavior! My initial concern was replacing an array with another one that might or might not be a different size, since it would be reading from a file of an arbitrary length. I decided to play with different things to see what all would happen. My first attempt was this:
public class RewriteArray {
public static void main(String[] args) {
String[] arr = { "1", "2", "3"};
System.out.print("Before rewrite: ");
for (String str : arr)
System.out.print(str + " ");
System.out.println();
changeArr(arr);
System.out.print("After rewrite: ");
for (String str : arr)
System.out.print(str + " ");
System.out.println();
}
private static void changeArr(String[] arr) {
arr[0] = "3";
arr[1] = "2";
arr[2] = "1";
}
}
This did what I expected, proving the change by referenced object, and gave the output:
Before rewrite: 1 2 3
After rewrite: 3 2 1
I then tried resizing the array within the changeArr
method, and that's when things got weird.
public class RewriteArray {
public static void main(String[] args) {
String[] arr = { "1", "2", "3"};
System.out.print("Before rewrite: ");
for (String str : arr)
System.out.print(str + " ");
System.out.println();
changeArr(arr);
System.out.print("After rewrite: ");
for (String str : arr)
System.out.print(str + " ");
System.out.println();
}
private static void changeArr(String[] arr) {
arr = new String[4];
arr[0] = "3";
arr[1] = "2";
arr[2] = "1";
arr[3] = "0";
}
}
I would have expected arr
to be replaced by a new object of the new size and then rewritten. Instead, I got:
Before rewrite: 1 2 3
After rewrite: 1 2 3
No errors or anything, but no changes in the array whatsoever! I then tried to resize the initial array by making a temporary one and re-assigning the pointer.
private static void changeArr(String[] arr) {
String[] tempArr = new String[4];
tempArr[0] = "3";
tempArr[1] = "2";
tempArr[2] = "1";
tempArr[3] = "0";
arr = tempArr;
}
Again, the output was identical to the previous version! No errors, but no changes either. Just to be completely sure it was at least POSSIBLE to resize an array, I tried creating a temporary array in a separate method and returning the array.
public class RewriteArray {
public static void main(String[] args) {
String[] arr = { "1", "2", "3"};
System.out.print("Before rewrite: ");
for (String str : arr)
System.out.print(str + " ");
System.out.println();
arr = loadArr();
System.out.print("After rewrite: ");
for (String str : arr)
System.out.print(str + " ");
System.out.println();
}
private static String[] loadArr() {
String[] tempArr = new String[4];
tempArr[0] = "3";
tempArr[1] = "2";
tempArr[2] = "1";
tempArr[3] = "0";
return tempArr;
}
}
This time it finally worked! I got the following output:
Before rewrite: 1 2 3
After rewrite: 3 2 1 0
It certainly seems at this point that it's best to have to create the new temporary array in the separate method and return that array, then assign the returned array to the original one. I just don't understand why it's not working with the changeArr
method internally. Isn't this passing references internally? If it wasn't, why would my first attempt work while the others didn't?