public static void main(String[] args) { Random rand = new Random(); int[] arr1 = new int[10]; int[] arr2 = new int[15]; for(int i = 0; i < 10; i++) { arr1[i] = rand.nextInt(50)+1; } Arrays.sort(arr1); for(int i = 0; i < 15; i++) { arr2[i] = rand.nextInt(50)+1; } Arrays.sort(arr2); /*---------以下是核心代码,对上面两个有序数组进行二路归并a*/ int i = 0; int j = 0; int k = 0; int len = arr1.length + arr2.length; int[] arr = new int[len]; while(i<arr1.length && j<arr2.length) { if(arr1[i]<=arr2[j]) { arr[k++] = arr1[i++]; }else { arr[k++] = arr2[j++]; } } if(i==arr1.length && j<arr2.length) { while(j<arr2.length) { arr[k++] = arr2[j++]; } } if(j==arr2.length && i<arr1.length) { while(i < arr1.length) { arr[k++] = arr1[i++]; } } String str = Arrays.toString(arr); System.out.println(str); }