Reputation: 105
Hello I've been trying to figure out this bug in my program for hours and I've been able to pin it down. I won't sit here and explain what my program is supposed to do since I've remade it with as little code as possible to replicate the bug, this code simply swaps the values of two arrays, for simplicity's sake I've made the arrays contain only one value, array1 has value of 1, array2 has value of 2.
Whenever the "BUGGED" commented line is activated instead of the one above the output is as follows:
Before swap: First value: [1] Second value: [2] After swap: First value: [2] Second value: [2]
If the first line is used the output is correct (after swap: 2 1).
I should add that if I make multiple arrays (any number really) the bug only occurs with the first and last value.
For example if I make 4 arrays with the value of 1, 2, 3 and 4 respectively and try to swap
temp < 1 < 2 < 3 < 4 < temp
I am going to have this situation at the end: 2, 3, 4, 2.
Here is the code of the main class.
class Testing {
int[] one = {1};
int[] two = {2};
TestingArray array1 = new TestingArray(one);
TestingArray array2 = new TestingArray(two);
public static void main(String[] args) {
Testing test = new Testing();
System.out.println("Before swap:");
System.out.println("First value: "+Arrays.toString(test.getArray1()));
System.out.println("Second value: "+Arrays.toString(test.getArray2()));
test.swap();
System.out.println("After swap:");
System.out.println("First value: "+Arrays.toString(test.getArray1()));
System.out.println("Second value: "+Arrays.toString(test.getArray2()));
}
int[] getArray1() {
return array1.getArray();
}
int[] getArray2() {
return array2.getArray();
}
void swap() {
int[] temp = array1.getArray();
array1.setArray(array2.getArray());
array2.setArray(temp);
}
}
And here is the second class.
public class TestingArray {
private int[] array = new int[1];
TestingArray (int[] value) {
this.array = value;
}
int[] getArray() {
return array;
}
void setArray(int[] array) {
this.array = array;
// this.array[0] = array[0]; // BUGGED
}
}
Really looking forward to your answers, I am really lost.
Upvotes: 3
Views: 70
Reputation: 6274
The problem is in the method swap()
, where your temp
variable is a reference of array1
not a copy as it should be if you want to change to values in the arrays:
void swap() {//e.g. arr1 → {1}, arr2 → {2}
int[] temp = array1.getArray(); //arr1 → {1} ← temp, arr2 → {2}
array1.setArray(array2.getArray()); //arr1 → {2} ← temp, arr2 → {2}
array2.setArray(temp); //arr1 → {2} ← temp, arr2 → {2} / no change
}
As you can see, when you call array1.setArray(array2.getArray())
you also change the value for temp
, because they reference the same array.
What you should do is:
void swap() {
int[] temp = Arrays.copyOf(array1.getArray(), array1.getArray().length); //arr1 → {1}, temp → {1}, arr2 → {2}
array1.setArray(array2.getArray()); //arr1 → {2}, temp → {1}, arr2 → {2}
array2.setArray(temp); //arr1 → {2}, temp → {1}, arr2 → {1}
}
You can read more about reference variables vs primitive variable in Java.
Upvotes: 2