java System.arraycopy()使用方法

 源码及参数说明

我们先看下arraycopy方法的源码,这是一个native方法,有5个参数

  • 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.
 * @exception  IndexOutOfBoundsException  if copying would cause
 *               access of data outside array bounds.
 * @exception  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.
 * @exception  NullPointerException if either <code>src</code> or
 *               <code>dest</code> is <code>null</code>.
 */
@FastNative
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);

使用方法

直接写几行代码,打印输出验证下

public class Test6 {
    public static void main(String[] args) {
        int[] src = {1, 2, 3};
        int[] dest = {4, 5, 6, 7, 8, 9};
        System.out.println(Arrays.toString(dest));
        // 拷贝到开始位置
        System.arraycopy(src, 0, dest, 0, src.length);
        System.out.println(Arrays.toString(dest));
        // 拷贝到末尾位置
        dest = new int[]{4, 5, 6, 7, 8, 9}; // 重新初始化数据,因为拷贝后是直接修改目的数据,而不是返回一个新的数组
        System.arraycopy(src, 0, dest, dest.length - src.length, src.length);
        System.out.println(Arrays.toString(dest));
        // 拷贝到中间位置
        dest = new int[]{4, 5, 6, 7, 8, 9};
        System.arraycopy(src, 0, dest, 2, src.length);
        System.out.println(Arrays.toString(dest));
        // 只拷贝源数组的部分元素
        dest = new int[]{4, 5, 6, 7, 8, 9};
        System.arraycopy(src, 1, dest, 0, 1);
        System.out.println(Arrays.toString(dest));
    }
}

输出结果如下

[4, 5, 6, 7, 8, 9]
[1, 2, 3, 7, 8, 9]
[4, 5, 6, 1, 2, 3]
[4, 5, 1, 2, 3, 9]
[2, 5, 6, 7, 8, 9]

异常

从源码可以看出若使用不当,arraycopy方法可能会抛出3种异常

  • ArrayIndexOutOfBoundsException:若拷贝的长度超过数组边界会抛出此异常

  • ArrayStoreException:若源数组的的某个元素无法存储在目的数组,会抛出次异常(类型不一致)

  • NullPointerException:若源数组或者目的数组为null,会抛此异常

ArrayIndexOutOfBoundsException异常

int[] src = {1, 2, 3};
int[] dest = {4, 5, 6, 7, 8, 9};
// 源数组下标越界,抛ArrayIndexOutOfBoundsException异常
System.arraycopy(src, -1, dest, 0, src.length);
// 源数组上标越界,抛ArrayIndexOutOfBoundsException异常
System.arraycopy(src, 0, dest, 0, 4);
// 目标数组下标越界,抛ArrayIndexOutOfBoundsException异常
System.arraycopy(src, 0, dest, -1, src.length);
// 目标数组上标越界(destPos+length>dest.length-1的情况下),抛ArrayIndexOutOfBoundsException异常
System.arraycopy(src, 0, dest, 5, src.length);

ArrayStoreException异常

public class Test6 {
    public static void main(String[] args) {
        double[] src = {1, 2, 3};
        int[] dest = {4, 5, 6, 7, 8, 9};
        // double类型数据不能拷贝到int类型数组,抛ArrayStoreException异常
        System.arraycopy(src, 0, dest, 0, src.length);
    }
}

 NullPointerException异常

public class Test6 {
    public static void main(String[] args) {
        double[] src = null;
        int[] dest = {4, 5, 6, 7, 8, 9};
        // src为null,抛NullPointerException异常
        System.arraycopy(src, 0, dest, 0, src.length);
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值