类名 ArrayMerging
类代码
import java.util.Arrays;
public class ArrayMerging {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("首先准备两个数组,他俩的长度是5-10之间的随机数,并使用随机数初始化这两个数组");
System.out.println("然后准备第三个数组,第三个数组的长度是前两个的和");
System.out.println("通过System.arraycopy 把前两个数组合并到第三个数组中");
int[] a = new int[8];
int[] b = new int[5];
int[] c = new int[a.length + b.length];
for (int i = 0; i < a.length; i++) {
a[i] = (int) (Math.random() * 5 + 5);
}
System.out.println("数组a的内容:" + Arrays.toString(a));
for (int i = 0; i < b.length; i++) {
b[i] = (int) (Math.random() * 5 + 5);
}
System.out.println("数组b的内容:" + Arrays.toString(b));
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
System.out.println("数组c的内容:" + Arrays.toString(c));
}
}
运行结果
首先准备两个数组,他俩的长度是5-10之间的随机数,并使用随机数初始化这两个数组
然后准备第三个数组,第三个数组的长度是前两个的和
通过System.arraycopy 把前两个数组合并到第三个数组中
数组a的内容:[6, 5, 5, 5, 6, 7, 7, 6]
数组b的内容:[9, 8, 6, 8, 5]
数组c的内容:[6, 5, 5, 5, 6, 7, 7, 6, 9, 8, 6, 8, 5]