修改作为方法参数传递的数组
假设我有一个 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的示例 1 和 3 在问题的上下文中实际上是相同的 - 您正在尝试为
n
分配一个新值(这是对按值传递的数组的引用)。您克隆
temp
数组的事实并不重要 - 它所做的只是创建temp
的副本,然后将其分配给n
。为了将值复制到传递给
method
方法的数组中,您可能需要查看:System.arraycopy这一切,当然,取决于
n
数组的大小以及您在method
方法中创建的数组的大小。例如,假设它们的长度相同,您可以这样做:
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 oftemp
and then assign it ton
.In order to copy values into array passed into your
method
method you might want to look at:System.arraycopyIt all, of course, depends on the sizes of your
n
array and the one you create insidemethod
method.Assuming they both have the same length, for example, you would do it like that:
在您的方法中,
n
是传入数组的另一个名称。它指向内存中与原始数组相同的位置,即一个整数数组。如果更改存储在该数组中的值之一,则指向该数组的所有名称都将看到更改。然而,在实际方法中,
您正在创建一个新数组,然后说“名称 'n' 现在指向这个数组,而不是传入的数组”。实际上,名称“n”不再是传入数组的名称。
In your method
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
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.
正如您正确地注意到的那样,您不能分配给作为参数传递的数组引用。 (或者更准确地说,赋值不会对调用者产生任何影响。)
这是您能做的最好的事情:
当然,只有当输入数组的大小与数组的大小相同时,这才可以正常工作。您正在复制到其中的数组。 (您应该如何处理这个问题将取决于应用程序......)
需要注意的是,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:
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.)
在您的
method
方法中,分配给n
的任何内容都不会更改传入并分配给n
的对象的值。在方法
的开头,n
指向一个数组。当您将n
分配为等于另一个数组时,您只需重新指向n
指向的数组,并且没有更改有关temp_array
的任何内容> 来自main
方法。In your
method
method, nothing that you assign ton
will ever change the value of the object passed in and assigned ton
. At the beginning ofmethod
,n
points to an array. When you assignn
to equal another array, you've simply re-pointed which arrayn
points to, and haven't changed anything abouttemp_array
from themain
method.