gpt4 book ai didi

java - 在java中没有for循环将数组拆分为两部分

转载 作者:IT老高 更新时间:2023-10-28 20:48:48 27 4
gpt4 key购买 nike

我有一个大小为 300000 的数组,我希望它把它分成 2 个相等的部分。有什么方法可以在这里实现这个目标吗?

会比for循环操作更快还是不会对性能造成影响?

最佳答案

您可以使用 System.arraycopy() .

int[] source = new int[1000];

int[] part1 = new int[500];
int[] part2 = new int[500];

// (src , src-offset , dest , offset, count)
System.arraycopy(source, 0 , part1, 0 , part1.length);
System.arraycopy(source, part1.length, part2, 0 , part2.length);

关于java - 在java中没有for循环将数组拆分为两部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5731042/

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