1. 程式人生 > >java陣列截斷和交換

java陣列截斷和交換

將陣列a的前5個複製到b,將a1的後4個複製到b,組合成新陣列b:

-------------------------------------------------------------------------------------------------------------------

int[] a = {2,2,2,2,2,0,0,0,0}; 

int[] a1 = {0,0,0,0,0,1,1,1,1}; 
int[] b=new int[9];      
//引數:陣列源,拷貝的起始下標,目標陣列,填寫的起始下標,拷貝的長度)
System.arraycopy(a, 0, b, 0, 5);
System.arraycopy(a1, 5, b, 5, 4);
for(int i=0;i<b.length;i++){
System.out.print(b[i]);

}

-------------------------------------------------------------------------------------------------------------------

輸出:222221111