public class combineArray {
/*
1.准备2个数组,a长度为5,b长度为8,并随机填充数;
2.准备数组c,长度是a.length+b.length;
3.使用System.arraycopy(src,srcPos,dest,destPos,length)把a,b合并到c
/
public static void main(String[]args){
int a[] = new int[5];
int b[] = new int[8];
int c[] = new int[a.length+b.length];
//填充数组a,b
System.out.print("数组a={");
for(int i=0;i<a.length;i++){
a[i] = (int)(Math.random()100);
System.out.print(a[i]+",");
}
System.out.println("}");
System.out.print("数组b={");
for(int j=0;j<b.length;j++){
b[j] = (int)(Math.random()100);
System.out.print(b[j]+",");
}
System.out.println("}");
System.arraycopy(a,0,c,0,a.length);
System.arraycopy(b,0,c,a.length,b.length);
System.out.print("数组c={");
for(int each:c){
System.out.print(each+",");
}
System.out.println("}");
}
}
网友评论