java中的数组复制

来源:互联网 发布:mac如何导出图片 编辑:程序博客网 时间:2024/05/22 11:52

在java中,两个数组a[10]和b[10],可以这样直接进行赋值:a=b。(c中是不行的)。这样数组复制的结果是:a是b的引用。即a做的修改就是b的修改。真正的“数组复制”应该是值复制而非地址复制,所以当用到数组复制的时候要看清楚要求。

java提供了一个函数:System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length),可以解决这个问题。当然也可以用for循环。


javadoc见于java.lang.System:

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)


src- the source array.

srcPos- starting position in the source array.

dest- the destination array.

destPos- starting position in the destination data.

length- the number of array elements to be copied.
0 0
原创粉丝点击