修改作为方法参数传递的数组

发布于 2024-11-30 07:26:57 字数 1024 浏览 1 评论 0原文

假设我有一个 int 数组并且我想修改它。我知道我无法将新数组分配给作为参数传递的数组:

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};
}

虽然我可以修改它:

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;
}

然后,我尝试使用 clone() 将任意数组分配给作为参数传递的数组:

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)
{
    int[] temp = new int[]{2};
    n = temp.clone();
}

现在,我想知道为什么它在最后一个示例中打印 1,而我只是用 clone() 复制数组,它只是复制值而不是引用。您能为我解释一下吗?


编辑:有没有办法将数组复制到对象而不更改引用?我的意思是让最后一个例子打印 2

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);
    System.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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

呢古 2024-12-07 07:26:57

您的示例 1 和 3 在问题的上下文中实际上是相同的 - 您正在尝试为 n 分配一个新值(这是对按值传递的数组的引用)。

您克隆 temp 数组的事实并不重要 - 它所做的只是创建 temp 的副本,然后将其分配给 n

为了将值复制到传递给 method 方法的数组中,您可能需要查看:System.arraycopy

这一切,当然,取决于n 数组的大小以及您在 method 方法中创建的数组的大小。

例如,假设它们的长度相同,您可以这样做:

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
}

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
}
终弃我 2024-12-07 07:26:57

在您的方法中,

public static void method(int[] n)

n 是传入数组的另一个名称。它指向内存中与原始数组相同的位置,即一个整数数组。如果更改存储在该数组中的值之一,则指向该数组的所有名称都将看到更改。

然而,在实际方法中,

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

您正在创建一个新数组,然后说“名称 'n' 现在指向这个数组,而不是传入的数组”。实际上,名称“n”不再是传入数组的名称。

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.

哆啦不做梦 2024-12-07 07:26:57

正如您正确地注意到的那样,您不能分配给作为参数传递的数组引用。 (或者更准确地说,赋值不会对调用者产生任何影响。)

这是您能做的最好的事情:

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
}

当然,只有当输入数组的大小与数组的大小相同时,这才可以正常工作。您正在复制到其中的数组。 (您应该如何处理这个问题将取决于应用程序......)


需要注意的是,Java 按值传递数组引用。它不按值传递数组内容。而克隆无助于解决这个问题。 (至少,没有声明的方法签名。)

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.)

送舟行 2024-12-07 07:26:57

在您的 method 方法中,分配给 n 的任何内容都不会更改传入并分配给 n 的对象的值。在方法的开头,n指向一个数组。当您将 n 分配为等于另一个数组时,您只需重新指向 n 指向的数组,并且没有更改有关 temp_array 的任何内容> 来自 main 方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文