System.arraycopy() 和 Arrays.copyOf()

描述

public static int[] copyOf(int[] original,int newLength)
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

作用

Arrays.copyOf()方法返回原始数组的副本,用零截断或填充以获取指定的长度。
System.arraycopy()方法复制指定的源数组的数组,在指定的位置开始,到目标数组的指定位置。

参数

copyOf()参数

  • original - 这是要复制的数组
  • newLength - 这是要返回的副本的长度

arraycopy()参数

  • Object src : 原数组
  • int srcPos : 从元数据的起始位置开始
  • Object dest : 目标数组
  • int destPos : 目标数组的开始起始位置
  • int length : 要copy的数组的长度

代码

测试环境

JDK 10

测试copyOf

    int arr1[] = { 0, 1, 2, 3, 4, 5 };
    int arr2[] = { 5, 10, 20, 30, 40, 50 };
    System.out.println("array = " + Arrays.toString(Arrays.copyOf(arr2, 3)));
    System.out.println("array = " + Arrays.toString(Arrays.copyOf(arr2, 7)));
    System.out.println(System.getProperty("java.version"));
    // 10.0.1

输出 :
- array = [5, 10, 20]
- array = [5, 10, 20, 30, 40, 50, 0]
- 10.0.1

测试arraycopy

    int arr0[] = { 10, 11, 12, 13, 14, 15 };
    int arr1[] = { 0, 1, 2, 3, 4, 5 };
    int arr2[] = { 5, 10, 20, 30, 40, 50 };

    // 将arr0中0开始共三位(0-2)复制到arr1中从0开始往后数3位
    System.arraycopy(arr0, 0, arr1, 0, 3);
    // 将arr2中0-5的数字用arr0中的前六位替换
    System.arraycopy(arr0, 0, arr2, 0, 6);

    // System.arraycopy(arr0, 0, arr2, 0, 8);
    // java.lang.ArrayIndexOutOfBoundsException

    System.out.println("array0 = " + Arrays.toString(arr0));
    System.out.println("array1 = " + Arrays.toString(arr1));
    System.out.println("array2 = " + Arrays.toString(arr2));
    System.out.println(System.getProperty("java.version"));

    /*
     * array0 = [10, 11, 12, 13, 14, 15] 
     * array1 = [10, 11, 12, 3, 4, 5] 
     * array2 = [10, 11, 12, 13, 14, 15] 
     * 10.0.1
     */

比较

copyOf源码

    /**
     * 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>.
     复制但不是原件,副本将包含<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(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }

测试代码

package fortest;

public class test {

    public static void main(String[] args) {
        testArrayCopyBytes();
        System.out.println(System.getProperty("java.version"));
        // - 10.0.1
    }

    public static void testHardCopyBytes() {
        byte[] bytes = new byte[0x5000000]; /* ~83mb buffer */
        byte[] out = new byte[bytes.length];
        for (int i = 0; i < out.length; i++) {
            out[i] = bytes[i];
        }
    }

    public static void testArrayCopyBytes() {
        // 伪代码
        long startTime = System.nanoTime(); // 获取开始时间
        testHardCopyBytes();
        long endTime = System.nanoTime(); // 获取结束时间
        System.out.println("程序运行时间: " + (endTime - startTime) + "ns");

        startTime = System.nanoTime();
        byte[] bytes = new byte[0x5000000]; /* ~83mb buffer */
        byte[] out = new byte[bytes.length];
        System.arraycopy(bytes, 0, out, 0, out.length);
        endTime = System.nanoTime();
        System.out.println("程序运行时间: " + (endTime - startTime) + "ns");

        // 第一次 : 
        // 程序运行时间: 170478773ns 
        // 程序运行时间: 114007147ns

        // 第二次 : 
        // 程序运行时间: 216062557ns 
        // 程序运行时间:  96864355ns

        // 第三次 : 
        // 程序运行时间: 172299936ns 
        // 程序运行时间: 109810835ns

        // 第四次 : 
        // 程序运行时间: 170478773ns 
        // 程序运行时间: 114007147ns

    }
}

总结

  1. System.arraycopy()Arrays.copyOf()更有优势,节省了寻址时间
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值