Question

I'm given the following method that is supposed to exchange the first two elements of the array that is passed as an argument to the method:

public void exchange(int[] a){
    int c[] = new int[a.length];
    c[0] = a[1];
    c[1] = a[0];
    for(int i = 2; i < a.length; i++)
        c[i] = a[i];
    a = c;
}

Why doesn't this method work as intended?

At first I thought i was because c's scope is limited to the method, a reference assignment a = c would not have any effect outside that function, but the method does not work even if I use c as an additional parameter. Can anybody tell me why this method doesn't work, please?

Was it helpful?

Solution

It doesnt work because a is just a copied reference to the array, alterations to a, like a[0] = 1; is reflected in the calling method, because you are changing the target of the reference (i.e the array) not the reference itself. a = c, does not work because it only changes the local reference to the array not the refernce in the calling method. See a more in depth explanation here: Is Java "pass-by-reference" or "pass-by-value"?

To exchange the first to values simply do:

int temp = a[0];
a[0] = a[1];
a[1] = temp;

OTHER TIPS

That's because in java arguments are always pass-by-value.

In your example you pass a reference to a object, however this reference is passed by value. In other words, new local variable a is created and initialized with the references to your array.

If you want to exchange two values you can do this:

public void exchange(int[] a){
    int temp = a[0];
    a[0] = a[1];
    a[1] = temp; 
}

But if you do this:

public void exchange(int[] a){        
    a = new int[10];    
}

then only the local variable a references the new array.

This method does not work as intended because this line

a = c;

changes the value of a reference to int[] passed into the exchange function without touching the referenced array, because object references are passed by value. If you want to change the content of a, switch its elements in place through a temporary variable, or copy c into a using the arrayCopy method or a loop.

Here is a very simple implementation that swaps the two initial elements of your array:

public void exchange(int[] a) {
    if (a.length < 2) return;
    int tmp = a[0];
    a[0] = a[1];
    a[1] = tmp;
}

Note the check for the minimum length that was missing from your code. It prevents exceptions when an array of zero or one elements is passed to your exchange method.

You can avoid having to change the reference by not creating a new array. This will be simpler and much, much faster.

public static void exchange(int[] a) {
    int tmp = a[0];
    a[0] = a[1];
    a[1] = tmp;
}

You can't change an argument in a called method in Java, you can only change an object which an argument points to.

You can also return the changed array:

public int[] exchange(int[] a){
    int c[] = new int[a.length];
    c[0] = a[1];
    c[1] = a[0];
    for(int i = 2; i < a.length; i++)
        c[i] = a[i];
    return c;
}

And use this method in this way:

a = exchange(a);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top