美文网首页
System.arraycopy

System.arraycopy

作者: 得鹿梦为鱼 | 来源:发表于2020-07-13 20:57 被阅读0次

最近被这个方法坑了下,特此放在这里,纪念!!!

/**
* @param      src      the source array. 源数组
* @param      srcPos   starting position in the source array. 源数组的起始位置
* @param      dest     the destination array. 目标数组
* @param      destPos  starting position in the destination data. 目标数组的起始位置
* @param      length   the number of array elements to be copied. 复制的长度
**/ 
 public static native void arraycopy(Object src,  int  srcPos, Object dest, int destPos, int length);

示例(实现移动位置的效果):

        int[] sources = new int[]{0, 1, 2, 3, 4, 5, 6, 7};
        // 目标是 将 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));

输出结果:


输出

相关文章

网友评论

      本文标题:System.arraycopy

      本文链接:https://www.haomeiwen.com/subject/ykuacktx.html