开发者

Modify an array passed as a method-parameter

开发者 https://www.devze.com 2023-03-29 04:36 出处:网络
Suppose I have an int-array and I want to modify it. I know that I cannot assign a new array to array passed as parameter:

Suppose I have an int-array and I want to modify it. I know that I cannot assign a new array to array passed as parameter:

public static void main(String[] args)
{
    int[] temp_array = {1};
    method(temp_array);
    System.out.println(temp_array[0]); // prints 1
}
public static void method(int[] n)
{
    n = new int[]{2};
}

while I can modify it:

public static void main(String[] args)
{
    int[] temp_array = {1};
    method(temp_array);
    System.out.println(temp_array[0]); // prints 2
}
public static void method(int[] n)
{
    n[0] = 2;
}

Then, I tried to assign an arbitrary array to the array passed as parameter using clone():

public static void main(String[] args)
{
    int[] temp_array = {1};
    method(temp_array);
    Syste开发者_JAVA技巧m.out.println(temp_array[0]); // prints 1 ?!
}
public static void method(int[] n)
{
    int[] temp = new int[]{2};
    n = temp.clone();
}

Now, I wonder why it prints 1 in last example while I'm just copying the array with clone() which it's just copying the value not the reference. Could you please explain that for me?


EDIT: Is there a way to copy an array to object without changing the reference? I mean to make last example printing 2.


Your examples 1 and 3 are virtually the same in context of the question - you are trying to assign a new value to n (which is a reference to an array passed by value).

The fact that you cloned temp array doesn't matter - all it did was create a copy of temp and then assign it to n.

In order to copy values into array passed into your method method you might want to look at:System.arraycopy

It all, of course, depends on the sizes of your n array and the one you create inside method method.

Assuming they both have the same length, for example, you would do it like that:

public static void main(String[] args)
{
    int[] temp_array = {1};
    method(temp_array);
    System.out.println(temp_array[0]);
}
public static void method(int[] n)
{
    int[] temp = new int[]{2};
    System.arraycopy(temp, 0, n, 0, n.length); 
    // or System.arraycopy(temp, 0, n, 0, temp.length) - 
    // since we assumed that n and temp are of the same length
}


In your method

public static void method(int[] n)

n is another name for the array that way passed in. It points to the same place in memory as the original, which is an array of ints. If you change one of the values stored in that array, all names that point to it will see the change.

However, in the actual method

public static void method(int[] n) {
    int[] temp = new int[]{2};
    n = temp.clone();
}

You're creating a new array and then saying "the name 'n' now points at this, other array, not the one that was passed in". In effect, the name 'n' is no longer a name for the array that was passed in.


As you correctly note, you cannot assign to the array reference passed as a parameter. (Or more precisely, the assignment won't have any effect in the caller.)

This is about the best that you can do:

public static void method(int[] n) {
    int[] temp = new int[]{2};
    for (int i = 0; i < temp.length; i++) {
        n[i] = temp[i];
    }
    // ... or the equivalent using System.arraycopy(...) or some such
}

Of course, this only works properly if the size of the input array is the same as the size of the array you are copying to it. (How you should deal with this will be application specific ...)


For the record Java passes the array reference by value. It doesn't pass the array contents by value. And clone won't help to solve this problem. (At least, not with the method signature as declared.)


In your method method, nothing that you assign to n will ever change the value of the object passed in and assigned to n. At the beginning of method, n points to an array. When you assign n to equal another array, you've simply re-pointed which array n points to, and haven't changed anything about temp_array from the main method.

0

精彩评论

暂无评论...
验证码 换一张
取 消