合并两个整数数组 [英] Combine two integer arrays
本文介绍了合并两个整数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法从两个数组中创建一个数组?例如
Is there a way to create a single array out of two? E.g.
int[] array1 = {1,2,3};
int[] array2 = {4,5,6};
int[] array1and2 = array1 + array2;
推荐答案
不能直接添加,必须新建一个数组,然后将每个数组复制到新的数组中.System.arraycopy 是一种可以用来执行此复制的方法.
You can't add them directly, you have to make a new array and then copy each of the arrays into the new one. System.arraycopy is a method you can use to perform this copy.
int[] array1and2 = new int[array1.length + array2.length];
System.arraycopy(array1, 0, array1and2, 0, array1.length);
System.arraycopy(array2, 0, array1and2, array1.length, array2.length);
无论 array1 和 array2 的大小如何,这都可以工作.
This will work regardless of the size of array1 and array2.
这篇关于合并两个整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文