3

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.

4
  • This code works fine, when I run it I get [2], [1] after the swap, what is the problem exactly? Commented Apr 1, 2016 at 2:07
  • @Maljam The point is that it works now, but doesn't work if he uses the commented statement instead. He wants to know why the commented statement doesn't work as expected. Commented Apr 1, 2016 at 2:09
  • Remember to clearly state the problem. Be sure that our users know exactly what is going wrong. It's better to say "when I call swap(1, 2) no values change", or something like that, than to say "it doesn't work, I'm lost". Also talk about any errors you receive. Commented Apr 1, 2016 at 2:11
  • @MatthewCliatt I think the question was pretty clear, people just aren't paying attention and are just copying the code. I did the exact same thing until I remembered the question. Commented Apr 1, 2016 at 2:20

1 Answer 1

2

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.

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

3 Comments

Thanks a lot man, this is the first time I've been stuck on an issue for so long I had to make a post somewhere. Just started learning Java and programming in general so this one really confused me. I would also appreciate if you would explain to me why the first line actually worked and the second one didnt, thanks again either way!
It took me awhile to understand why this was the solution. It's because when you only make a referential copy, then any modification of array1 will also be a modification of temp, which corrupts the subsequent modification of array2 which uses the new array1 value instead of the old array1 value. (I feel dizzy)
@Daniel I added more explanation, hope it helps

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.