使阵列的Java副本 [英] Make copy of array Java

查看:153
本文介绍了使阵列的Java副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组A中不断更新。比方说,A = [1,2,3,4,5]。我需要作出的精确副本并将其命名为B.如果是更改为[6,7,8,9,10],B仍然应该是[1,2,3,4,5]。什么是做到这一点的最好方法是什么?我尝试了像循环:

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.

推荐答案

您可以尝试使用<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy%28java.lang.Object,%20int,%20java.lang.Object,%20int,%20int%29\">System.arraycopy()

int[] src  = new int[]{1,2,3,4,5};
int[] dest = new int[5];

System.arraycopy( src, 0, dest, 0, src.length );

这篇关于使阵列的Java副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆