制作数组的副本 [英] Make copy of array
问题描述
我有一个数组 a
,它不断被更新。让我们说 a = [1,2,3,4,5]
。我需要制作 a
的完全重复副本,并将其命名为 b
。如果 a
要更改为[6,7,8,9,10], b
仍应为[1 ,2,3,4,5]。做这个的最好方式是什么?我尝试了一个for循环,如:
I have an array a
which is constantly being updated. Lets say a = [1,2,3,4,5]
. I need to make an exact duplicate copy of a
and call it b
. If a
were to change to [6,7,8,9,10], b
should still be [1,2,3,4,5]. What is the best way to do this? I tried a for loop like:
for(int i=0; i<5; i++){
b[i]=a[i]
}
但这似乎无法正常工作。请不要使用深层复制等高级术语,因为我不知道这意味着什么。
but that doesn't seem to work correctly. Please don't use advanced terms like deep copy etc because I do not know what that means.
推荐答案
您可以尝试使用< a href =http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy%28java.lang.Object,%20int,%20java.lang.Object,%20int ,%20int%29rel =noreferrer> System.arraycopy()
int[] src = new int[]{1,2,3,4,5};
int[] dest = new int[5];
System.arraycopy( src, 0, dest, 0, src.length );
这篇关于制作数组的副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!