arraycopy方法簡析

arraycopy(System類的靜態方法)

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

簡述

從指定源數組中複製一個數組,複製從指定的位置開始,到目標數組的指定位置結束。

參數:

src - 源數組。
srcPos - 源數組中的起始位置。
dest - 目標數組。
destPos - 目標數據中的起始位置。
length - 要複製的數組元素的數量。

public static void main(String[] args) {
        int []one={1,2,3,0,0,0,0};
        int []two={4,5,6,7};
        
        //將數組two中元素從索引0開始長度爲4,複製到數組one中從索引3開始
        System.arraycopy(two,0,one,3,4);
        System.out.println("數組one爲:"+Arrays.toString(one));
        System.out.println();
        
        //數組one現在爲[1, 2, 3, 4, 5, 6, 7]
        //將數組one中的索引從0開始長度爲3,複製到數組one的索引從4開始
        System.arraycopy(one,0,one,4,3);
        System.out.println("數組one爲:"+Arrays.toString(one));
    }

在這裏插入圖片描述
Tips:arraycopy是System類的靜態方法,使用System.arraycopy(xxx)調用
ArrayList的remove()和add(int ?,object ?)都是是根據此方法進行的操作。

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