System.arraycopy

最近被這個(gè)方法坑了下,特此放在這里,紀(jì)念!!!

/**
* @param      src      the source array. 源數(shù)組
* @param      srcPos   starting position in the source array. 源數(shù)組的起始位置
* @param      dest     the destination array. 目標(biāo)數(shù)組
* @param      destPos  starting position in the destination data. 目標(biāo)數(shù)組的起始位置
* @param      length   the number of array elements to be copied. 復(fù)制的長度
**/ 
 public static native void arraycopy(Object src,  int  srcPos, Object dest, int destPos, int length);

示例(實(shí)現(xiàn)移動(dòng)位置的效果):

        int[] sources = new int[]{0, 1, 2, 3, 4, 5, 6, 7};
        // 目標(biāo)是 將 5 移到 0 去,也就是輸出  5,0,1,2,3,4,6,7
        int srcPos = 0;
        int destPos = 5;
        int destValue = sources[destPos];

        System.out.println(Arrays.toString(sources));

        System.arraycopy(sources, srcPos, sources, srcPos + 1, destPos - srcPos);
        sources[srcPos] = destValue;

        System.out.println(Arrays.toString(sources));

輸出結(jié)果:


輸出
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容