gpt4 book ai didi

java - 在 Java 中复制大型数组的最有效方法

转载 作者:太空狗 更新时间:2023-10-29 15:35:05 25 4
gpt4 key购买 nike

我想复制一个大的整数数组,例如从数组 a 到数组 b。我找到了几种方法来做到这一点,包括:

int[] a = new int[]{1,2,3,4,5};
int[] b = new int[5];

System.arraycopy( a, 0, b, 0, a.length );

int[] a = new int[]{1,2,3,4,5};
int[] b = (int[])a.clone();

由于这是在移动设备上完成的,我希望能够最有效地完成。

请告诉我最好的方法。

最佳答案

System.arraycopy 是更好的方法。

原因:它是通过本地代码实现的,因此效率更高。 Josh Bloch 建议(在 Effective Java 中)避免使用 clone() 方法来复制/克隆对象。

请引用:Effective Java: Analysis of the clone() method

关于java - 在 Java 中复制大型数组的最有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14723748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com