java中的數組複製

在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.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章