利用LinkedList将两个数组合并成一个有序的链表
突然发现用ArrayList会更方便
然后在对新的表操作,找到中位数
public static double m7(){
int[] nums1 = {1,3}; // j
int[] nums2 = {1, 4, 7, 8, 13, 14, 16}; // i
List<Double> list = new LinkedList<>();
int i = 0,j = 0;
for (;i < nums2.length && j < nums1.length;) {
if(nums1[j] >= nums2[i]){
list.add((double) nums2[i]);
i++;
} else if (nums1[j] < nums2[i]) {
list.add((double) nums1[j]);
j++;
}
}
if(nums2.length > nums1.length){
for (int k = i; k < nums2.length; k++) {
list.add((double) nums2[k]);
}
} else if (nums2.length < nums1.length) {
for (int k = j; k < nums1.length; k++) {
list.add((double) nums1[k]);
}
}
System.out.println(list);
int a = list.size();
System.out.println("a = " + a );
if(a % 2 == 0){
System.out.println("aaaaa");
double b = list.get(a/2);
double c = list.get(a/2 + 1);
return (b + c) / 2;
} else if (a % 2 != 0) {
System.out.println("bbbbbbb");
return list.get(a/2);
}
return 0;
}