java的数组复制方法System.arraycopy()的使用说明

使用方法

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

参数:

  • src:要复制的数组(源数组)
  • srcPos:复制源数组的起始位置
  • dest:目标数组
  • destPos:目标数组的下标位置
  • length:要复制的长度

源码

/**
     * @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. 要复制的长度
     * @throws IndexOutOfBoundsException if copying would cause
     *                                   access of data outside array bounds.
     *                                   如果复制会导致数据的访问超出数组边界。
     *                                   则会报IndexOutOfBoundsException索引越界异常
     * @throws ArrayStoreException       if an element in the <code>src</code> array
     *                                   could not be stored into the <code>dest</code> array
     *                                   because of a type mismatch.
     *                                   如果由于类型不匹配而无法将src数组中的元素存储到dest数组中。
     *                                   则会报 ArrayStoreException数组存储异常
     * @throws NullPointerException      if either <code>src</code> or
     *                                   <code>dest</code> is <code>null</code>.
     *                                   如果src或dest为null。
     *                                   则会报NullPointerException空指针异常
     */

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

示例代码

public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5};
        int[] b = new int[5];
        System.out.println(Arrays.toString(b)); //输出[0, 0, 0, 0, 0]
        System.arraycopy(a, 0, b, 0, 5); //把a复制到b
        System.out.println(Arrays.toString(b)); //输出[1, 2, 3, 4, 5]


        /*int[] a1 = {1,2,3,4,5};
        int[] b1 = new int[3];
        System.arraycopy(a1, 0, b1, 0, 5); //把a1复制到b1
        //因为b1的容量小于a1,则会报数组索引越界异常ArrayIndexOutOfBoundsException*/

        /*int[] a2 = {1,2,3,4,5};
        double[] b2 = new double[5];
        System.arraycopy(a2, 0, b2, 0, 5); //把a1复制到b1
        //因为a1和b1的数组类型不匹配,则会报数组存储异常ArrayStoreException*/
    }

System.arraycopy()的几种常用方法

1. 从旧数组拷贝到新数组

//从旧数组拷贝到新数组
for (int i=0;i<size;i++){
	arrayNew[i]=array[i];
}
System.arraycopy(array, 0, arrayNew, 0, array.length);

2. 从左向右循环,逐个元素向左挪一位。

//从左向右循环,逐个元素向左挪一位。
for (int i = index; i < size - 1; i++) {
	array[i] = array[i + 1];
}
System.arraycopy(array, index + 1, array, index, size - 1 - index);

3. 从右向左循环,逐个元素向右挪一位。

//从右向左循环,逐个元素向右挪一位。
for (int i = size - 1; i >= index; i--) {
	array[i + 1] = array[i];
}
System.arraycopy(array, index, array, index + 1, size - index);
//从右向左循环,逐个元素向右挪一位。
for (int i = size - 1; i >= index; i--) {
	array[i + 1] = array[i];
}
System.arraycopy(array, index, array, index + 1, size - index);
posted @ 2020-04-30 18:28  爱做梦的子浩  阅读(724)  评论(0编辑  收藏  举报