System.arrayCopy()与Arrays.copyOf()的区别

作为一个Android开发者,对这两个方法挺陌生的,只有在分析阅读ArrayList的源码才算是真正接触到这两个方法。在此记录下这两个方法的用法及其源码。

System.arrayCopy:

将指定源数组中的数组从指定位置复制到目标数组的指定位置。

实例:

package zzw.cn.listtest;

/**
 * @author 鹭岛猥琐男
 * @create 2019/8/7 19:55
 */
public class TestSystemArrayCopy
{
    public static void main(String[] args)
    {
        int[] src = {1, 2, 3, 4, 5};
        int[] dest = {6, 7, 8, 9, 10,11,12,13};
        System.out.println("源数组src:");
        for (int i = 0; i < src.length; i++)
        {
            if (i != src.length - 1)
            {
                System.out.print(src[i] + ",");
            } else
            {
                System.out.print(src[i]);
            }
        }
        //调用System.arraycopy
        System.arraycopy(src, 0, dest, 2, 3);
        System.out.println("\n目标数组arr:");
        for (int i = 0; i < dest.length; i++)
        {
            if (i != dest.length - 1)
            {
                System.out.print(dest[i] + ",");
            } else
            {
                System.out.print(dest[i]);
            }
        }
    }
}

 结果:

源数组src:
1,2,3,4,5
目标数组arr:
6,7,1,2,3,11,12,13
Process finished with exit code 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.
     * @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>.
     */
    public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);

此方法是一个native方法,无法看到具体的实现 。几个参数的含义:

src:源数组; 

srcPos:源数组要复制的起始位置;

dest:目的数组; 

destPos:目的数组放置的起始位置; 

length:复制的长度。

 

Arrays.copyOf:

Arrays.copyOf()不仅仅只是拷贝数组中的元素,在拷贝元素时,会创建一个新的数组对象。

实例:

package zzw.cn.listtest;

import java.util.Arrays;

/**
 * @author 鹭岛猥琐男
 * @create 2019/8/7 19:42
 */
public class Test
{
    public static void main(String[] args)
    {
        //原数组
        int[] arr = {1, 2, 3, 4, 5};
        System.out.println("原数组arr:");
        for (int i = 0; i < arr.length; i++)
        {
            if (i != arr.length - 1)
            {
                System.out.print(arr[i] + ",");
            } else
            {
                System.out.print(arr[i]);
            }
        }
        //对原数组arr复制,产生一个新的数组
        int[] arrNew = Arrays.copyOf(arr, 10);
        System.out.println("\n新数组arr:");
        for (int i = 0; i < arrNew.length; i++)
        {
            if (i != arrNew.length - 1)
            {
                System.out.print(arrNew[i] + ",");
            } else
            {
                System.out.print(arrNew[i]);
            }
        }
    }
}

运行结果:

原数组arr:
1,2,3,4,5
新数组arr:
1,2,3,4,5,0,0,0,0,0
Process finished with exit code 0

源码:

    /*
     * @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(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }

几个参数的含义:

original:要复制的数组

newLength :要返回副本的长度

从源码可以看出,首先创建一个数组copy ,此数组的长度为要返回副本的长度(newLength),接着调用 System.arraycopy 将要复制数组复制到新创建的数组 copy 中。复制从要复制的数组 original 的第一位开始,复制到目标数组 copy 中。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Arrays.copyOf() 和 System.arraycopy() 都是用于数组的复制操作,但它们有一些不同之处。 Arrays.copyOf() 方法是在 Java 1.6 版本中引入的,它用于创建一个新的数组,并将源数组中的元素复制到新数组中。该方法具有以下两种重载形式: 1. copyOf(original, newLength):将原始数组的前 newLength 个元素复制到新数组中。如果新长度小于原始数组的长度,则新数组将被截断;如果新长度大于原始数组的长度,则新数组将被填充默认值。 2. copyOf(original, newLength, newType):与上述方法相似,但可以指定新数组的类型。 示例使用 Arrays.copyOf() 方法: ```java int[] original = {1, 2, 3, 4, 5}; int[] copy1 = Arrays.copyOf(original, 3); // 复制原数组的前三个元素 int[] copy2 = Arrays.copyOf(original, 7); // 复制原数组的所有元素,并用默认值填充额外位置 ``` System.arraycopy() 方法是在 Java 1.0 版本中引入的,它也用于将源数组中的元素复制到目标数组中。该方法的语法如下: ```java System.arraycopy(src, srcPos, dest, destPos, length); ``` 其中,src 是源数组,srcPos 是源数组中要开始复制的起始位置,dest 是目标数组,destPos 是目标数组中要开始粘贴的起始位置,length 是要复制的元素个数。 示例使用 System.arraycopy() 方法: ```java int[] src = {1, 2, 3, 4, 5}; int[] dest = new int[5]; System.arraycopy(src, 0, dest, 0, 5); // 复制 src 数组的所有元素到 dest 数组中 ``` 总结来说,Arrays.copyOf() 方法提供了更简洁的方式来复制数组,并且可以轻松地截取或填充数组。而 System.arraycopy() 方法则提供了更灵活的复制方式,可以指定复制的起始位置和复制的元素数量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值