Java中数组复制的四种方法

本文介绍了四种数组复制方法:for循环、Object.clone()、Arrays.copyOf()和System.arraycopy(),并通过实验证明了System.arraycopy()在百万数据级别的情况下拥有最佳性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数组复制的方法

  1. for()循环方法;
  2. Object.clone()方法;
  3. Arrays.copyOf()方法;
  4. System.arraycopy()方法(推荐使用)
  5. 方法性能比较

1、for()循环方法

代码灵活,但效率低。


2、Object.clone()方法

从源码来看同样也是native方法,但返回为Object类型,所以赋值时将发生强转,所以效率不如之后两种。
(仅仅是在百万数据级别的情况下)

protected native Object clone() throws CloneNotSupportedException;

3、Arrays.copyOf()方法

同样看源码,它的实现还是基于System.arraycopy(),所以效率自然低于System.arraycpoy()。
(仅仅是在百万数据级别的情况下)

public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
        @SuppressWarnings("unchecked")
        T[] copy = ((Object)newType == (Object)Object[].class)
            ? (T[]) new Object[newLength]
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }

4、System.arraycopy()方法(推荐使用)

通过源码可以看到,其为native方法,即原生态方法。自然效率更高。
(仅仅是在百万数据级别的情况下)

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

5、方法性能测试比较

import java.util.Arrays;
import java.util.Random;

public class Main {
    public static void main(String[] args) {
        //int length = 10000000;    //千万级别
        int length = 8000000;       //百万级别
        Integer[] arr = new Integer[length];
        Integer[] arr2 = new Integer[length];
        for (int index = 0; index < length; index++) {
            arr[index] = new Random().nextInt(length) + 1;
        }

        //for()循环方法耗费时间:30ms
        long start = System.currentTimeMillis();
        for (int index = 0; index < length; index++) {
            arr2[index] = arr[index];
        }
        long end = System.currentTimeMillis();
        System.out.println("for()循环方法耗费时间:" + (end - start) + "ms");

        //Object.clone()方法耗费时间:15ms
        start = System.currentTimeMillis();
        arr2 = arr.clone();
        end = System.currentTimeMillis();
        System.out.println("Object.clone()方法耗费时间:" + (end - start) + "ms");

        //Arrays.copyOf()方法耗费时间:14ms
        start = System.currentTimeMillis();
        arr2 = Arrays.copyOf(arr, length);
        end = System.currentTimeMillis();
        System.out.println("Arrays.copyOf()方法耗费时间:" + (end - start) + "ms");

        //System.arraycopy()方法耗费时间:10ms
        start = System.currentTimeMillis();
        System.arraycopy(arr, 0, arr2, 0, length);
        end = System.currentTimeMillis();
        System.out.println("System.arraycopy()方法耗费时间:" + (end - start) + "ms");
    }
}

数据量是百万级别的情况下:
这里写图片描述

数据量是千万级别的情况下:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值