package 机试; import java.util.Arrays; /* * 在Java程序中,定义两个数组,首先把两个数组合并成一个新数组,然后把新数组中的所有元素逆序排列*/ public class Nixu { public static void main(String[] args) { int[] array1 = {10, 20, 30, 40, 50}; int[] array2 = {60, 70, 80, 90, 100}; // 合并两个数组 System.out.println("数组1:"+Arrays.toString(array1)); System.out.println("数组2:"+Arrays.toString(array2)); int[] mergedArray = Arrays.copyOf(array1, array1.length + array2.length); System.arraycopy(array2, 0, mergedArray, array1.length, array2.length); // 输出合并后的数组 System.out.println("合并后的数组是:" + Arrays.toString(mergedArray)); // 逆序排列新数组 for (int i = 0; i < mergedArray.length / 2; i++) { int temp = mergedArray[i]; mergedArray[i] = mergedArray[mergedArray.length - 1 - i]; mergedArray[mergedArray.length - 1 - i] = temp; } // 输出逆序排列后的新数组 System.out.println("逆序排列后的新数组:"+Arrays.toString(mergedArray)); } }
12-27
537