arrays.copyof和system.arraycopy的区别和联系

system.arraycopy是复制数组中制定元素到目标数组,

arrays.copyof会创建一个新的数组,并调用了system.arraycopy,具体见源码

public static int[] copyOf(int[] original, int newLength) {
        int[] copy = new int[newLength];
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }

关键点,arrays.copyof会从创建一个新的数组,而system.arraycopy只是复制数组的功能,单独的他不会创建新的数组。

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