public class Test2 {
public static void main(String[] args) {
int []arr1 = new int[5];
int []arr2 = new int[5];
name(arr1);
name(arr2);
System.out.println("原數組1: "+Arrays.toString(arr1));
System.out.println("原数组2: "+Arrays.toString(arr2));
int []arr =unionArr(arr1, arr2);
}
public static int[] unionArr(int[] arr1,int[] arr2){
int []arr = new int[arr1.length*2];
System.arraycopy(arr1, 0, arr, 0, arr1.length);
System.arraycopy(arr2, 0, arr, arr1.length, arr2.length);
System.out.println("合并后数组:"+Arrays.toString(arr));
return arr;
}
public static boolean arrTest(int []arr1,int num){
Random random = new Random();
for (int i = 0; i < arr1.length; i++) {
if (arr1[i]== num) {
return true;
}
}
return false;
}
public static void name(int []arr1) {
int index = 0;
Random random = new Random();
while(index<arr1.length) {
int num = random.nextInt(100)+1;
boolean flag =arrTest(arr1, num);
if (!flag&&num%2==0) {
arr1[index] = num;
index++;
}
}
}
}
|
|