数组的拷贝


数组的拷贝有四种常用的方法

对于一位数组来说都是浅拷贝,如果是多维数组,需要进行递推拷贝或者通过反序列化,相关序列化克隆内容在
对象的克隆

clone

该类是通过object的clone方法进行拷贝数组的

public class CopyArray {
	public static void main(String[] args) {
		int[] intArray = {1,2,3,4,5,6,7,8,9};
		
		int[] cloneArray = intArray.clone(); 使用clone方法进行克隆;
	}
}

这里为什么可以使用clone()方法呢,难道数组类实现了cloneable接口?
1、Java中并不存在任何一个类对应数组,数组属于Java语言的一部分。
2、数据是特殊的对象,本身就实现了Cloneable。Object的clone方法的javadoc中有这么一句Note that all arrays are considered to implement the interface Cloneable ,所以数组是可以直接使用clone方法的。
3、数组对象天生就有一个final的length属性,因为数组并没有定义在任何一个类中,所以没有源码。

System.arraycopy

public class CopyArray {
	public static void main(String[] args) {
		int[] sysArrayCopy = new int[intArray.length];、
		//arraycopy是操作系统的arraycopy方法~
		System.arraycopy(intArray, 0, sysArrayCopy, 0, intArray.length); 
	}
}

Arrays.copyOf

public class CopyArray {
	public static void main(String[] args) {
		//Arrays类封装的copyOf方法,底层原理依然是System.arraycopy()类
		int[] arrayCopyOf = Arrays.copyOf(intArray, intArray.length);
	}
}

底层源码

 /**
     * Copies the specified array, truncating or padding with zeros (if necessary)
     * so the copy has the specified length.  For all indices that are
     * valid in both the original array and the copy, the two arrays will
     * contain identical values.  For any indices that are valid in the
     * copy but not the original, the copy will contain <tt>0</tt>.
     * Such indices will exist if and only if the specified length
     * is greater than that of the original array.
     *
     * @param original the array to be copied
     * @param newLength the length of the copy to be returned
     * @return a copy of the original array, truncated or padded with zeros
     *     to obtain the specified length
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
     * @throws NullPointerException if <tt>original</tt> is null
     * @since 1.6
     */
    public static int[] copyOf(int[] original, int newLength) {
        int[] copy = new int[newLength];              //提前设置了数组,之后System.arraycopy(),将之返回;
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }

Arrays.copyOfRange

public class CopyArray {
	public static void main(String[] args) {
		//Arrays类封装的copyOfRange方法,底层原理依然是System.arraycopy()类
		int[] copyOfRange = Arrays.copyOfRange(intArray, 0, intArray.length);
	}
}

底层源码

/**
     * Copies the specified range of the specified array into a new array.
     * The initial index of the range (<tt>from</tt>) must lie between zero
     * and <tt>original.length</tt>, inclusive.  The value at
     * <tt>original[from]</tt> is placed into the initial element of the copy
     * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
     * Values from subsequent elements in the original array are placed into
     * subsequent elements in the copy.  The final index of the range
     * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
     * may be greater than <tt>original.length</tt>, in which case
     * <tt>0</tt> is placed in all elements of the copy whose index is
     * greater than or equal to <tt>original.length - from</tt>.  The length
     * of the returned array will be <tt>to - from</tt>.
     *
     * @param original the array from which a range is to be copied
     * @param from the initial index of the range to be copied, inclusive
     * @param to the final index of the range to be copied, exclusive.
     *     (This index may lie outside the array.)
     * @return a new array containing the specified range from the original array,
     *     truncated or padded with zeros to obtain the required length
     * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
     *     or {@code from > original.length}
     * @throws IllegalArgumentException if <tt>from &gt; to</tt>
     * @throws NullPointerException if <tt>original</tt> is null
     * @since 1.6
     */
    public static int[] copyOfRange(int[] original, int from, int to) {
        int newLength = to - from;
        if (newLength < 0)
            throw new IllegalArgumentException(from + " > " + to);
        int[] copy = new int[newLength];
        System.arraycopy(original, from, copy, 0,
                         Math.min(original.length - from, newLength));
        return copy;
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值