笔试题:
有M个整数数组,都已经从大到小排好序。请写出程序把这M个数组合成一个数组,也从大到小排序。
public class MMArrays {
public static void main(String[] args) {
//给定数组
int[] arr1 = new int[]{500, 400, 300, 200, 100};
int[] arr2 = {555, 444, 333, 222, 111};
int[] arr3 = {520, 432, 311, 250, 110};
int length = arr1.length + arr2.length + arr3.length;
int[] arr = new int[length];
//遍历数组
for (int i = 0; i < arr1.length; i++) {
arr[i] = arr1[i];
}
for (int i = 0; i < arr2.length; i++) {
arr[i + arr1.length] = arr2[i];
}
for (int i = 0; i < arr3.length; i++) {
arr[i + arr1.length + arr2.length] = arr3[i];
}
//排序前
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("----------------");
//升序排序
Arrays.sort(arr);
//降序(反向遍历)
for (int i = arr.length - 1; i >= 0; i--) {
System.out.println(arr[i]);
}
}
}