随机生成两个数组,并将他们拷贝合并到一起:
package qqq;
public class HelloWorld {
public static void main(String[] args) { int num1 = (int)(Math.random()*10); System.out.printf("随机生成的数组a长度为:%d",num1); int num2 = (int)(Math.random()*10); System.out.printf("\n随机生成的数组b长度为:%d",num2); int num3 = num1 + num2; System.out.printf("\n合并之后的数组c长度为:%d",num3); int[] a = new int[num1]; int[] b = new int[num2]; int[] c = new int[num3]; for (int i = 0; i < a.length; i++) { a[i] = (int)(Math.random()*100); } for (int i = 0; i < b.length; i++) { b[i] = (int)(Math.random()*100); } System.out.println("\n打印随机生成的a数组:"); for (int i = 0; i < a.length; i++) { System.out.printf("%d ",a[i]); } System.out.println("\n打印随机生成的b数组:"); for (int i = 0; i < b.length; i++) { System.out.printf("%d ",b[i]); } System.arraycopy(a, 0, c, 0, num1); System.arraycopy(b, 0, c, num1, num2); System.out.println("\n打印合并之后的c数组:"); for (int i = 0; i < c.length; i++) { System.out.printf("%d ",c[i]); } }}
运行结果:
领取专属 10元无门槛券
私享最新 技术干货