java System.arrayCopy使用說明


java System.arrayCopy使用說明

java.lang.System.arraycopy() 方法復制指定的源數組的數組,在指定的位置開始,到目標數組的指定位置。

 

下面是 System.arrayCopy的源代碼聲明 : 

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
代碼解釋:
  Object src : 原數組
int srcPos : 從元數據的起始位置開始
  Object dest : 目標數組
  int destPos : 目標數組的開始起始位置
  int length : 要copy的數組的長度

比如 :我們有一個數組數據 byte[]  srcBytes = new byte[]{2,4,0,0,0,0,0,10,15,50};  // 源數組

                                    byte[] destBytes = new byte[5]; // 目標數組

我們使用System.arraycopy進行轉換(copy)

System.arrayCopy(srcBytes,0,destBytes ,0,5)
上面這段代碼就是 : 創建一個一維空數組,數組的總長度為 12位,然后將srcBytes源數組中 從0位 到 第5位之間的數值 copy 到 destBytes目標數組中,在目標數組的第0位開始放置.
那么這行代碼的運行效果應該是 2,4,0,0,0,
我們來運行一下
1         byte[]  srcBytes = new byte[]{2,4,0,0,0,0,0,10,15,50}; 2         byte[] destBytes = new byte[5]; 3         System.arraycopy(srcBytes, 0, destBytes, 0, 5); 4         for(int i = 0;i< destBytes.length;i++){ 5             System.out.print("-> " + destBytes[i]); 6         } 

運行結果  : -> 2-> 4-> 0-> 0-> 0

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM