[英]Java shallow copy array
if this is a shallow copy 如果这是浅表副本
double[] a = new double[100];
a = b; // let b be some other double array[100]
I understand that the better way to do this is using a for loop or using 我知道更好的方法是使用for循环或使用
System.arrayCopy(b,0,a,0,100);
However what happens to this? 但是,这会发生什么呢?
public double[] function1(){
returns somedouble[100];
}
double[] a = new double[100];
a = function1(); // i believe this will also be a shallow copy
System.arrayCopy(function1(),0,a,0,100); // will this call function1 100 times?
double[] a = new double[100];
a = b; // let b be some other double array[100]
Create an array named a
of double with size of 100. Now when you a = b
will copy the reference of b array to variable a. 创建一个名为a
的double数组,其大小为100。现在,当a = b
会将b数组的引用复制到变量a。
+--------------------------------------------+ <- Suppose whole
a-> | 2.5 | | | | | | | | | | | | | | array is filled
+--------------------------------------------+ with value 2.5
+--------------------------------------------+ <- Suppose whole
b-> | 7.9 | | | | | | | | | | | | | | array is filled
+--------------------------------------------+ with value 7.9
after a = b 在a = b之后
+--------------------------------------------+ <- Suppose whole
| 2.5 | | | | | | | | | | | | | | array is filled
+--------------------------------------------+ with value 2.5
a-> +--------------------------------------------+ <- Suppose whole
b-> | 7.9 | | | | | | | | | | | | | | array is filled
+--------------------------------------------+ with value 7.9
So now a and b pointing to same array. 所以现在a和b指向相同的数组。
public double[] function1(){
return somedouble[100];
}
double[] a = new double[100];
a = function1();
Now same thing is happening here. 现在,同样的事情在这里发生。 You create an array named a and then call function1()
and again assigned the returning array reference to a. 创建一个名为a的数组,然后调用function1()
并再次将返回的数组引用分配给a。
System.arraycopy(function1(), 0, a, 0, 100);
Here the calling order will be 这里的呼叫顺序将是
1 -> function1() will be called and the returning array reference will be saved in a temporary variable. 1-> function1()将被调用,返回的数组引用将保存在一个临时变量中。
2 -> call to System.arraycopy(temporary variable, 0, a, 0, 100)
2->调用System.arraycopy(temporary variable, 0, a, 0, 100)
So function1()
will be called only once. 因此function1()
将仅被调用一次。
As a side note make sure you use System.arraycopy(args)
instead of System.arrayCopy(args)
作为附带说明,请确保使用System.arraycopy(args)
而不是System.arrayCopy(args)
double[] a = new double[100];
a = b; // let b be some other double array[100]
First, It's assigning, not copying. 首先,它是分配,而不是复制。
double[] a = new double[100];
a = function1(); // i believe this will also be a shallow copy
It's assigning. 它正在分配。 you assign the returned value somedouble[100] to a 您将返回值somedouble [100]分配给
System.arrayCopy(function1(),0,a,0,100); System.arrayCopy(function1(),0,a,0,100); // will this call function1 100 times? //此函数会调用100次100次吗?
No, it won't call function1 100 times. 不,它不会调用function1 100次。 The above code mostly equals to 上面的代码大部分等于
double[] tmpref = function1();
System.arrayCopy(tmparr,0,a,0,100);
Because it first evaluates the arguments, then it calls arrayCopy 因为它首先计算参数,然后调用arrayCopy
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.