package Fri;
import java.util.Arrays;
//两个有序数组的合并
public class Sat04 {
public static void main(String[] args) {
int[] s1 = { 1, 3, 5, 7, 9 };
int[] s2 = { 2, 4, 6, 8, 10, 12, 14, 16 };
int[] s = new int[s1.length + s2.length];
int i = 0, p1 = 0, p2 = 0;
// 方式2
while (p1 < s1.length && p2 < s2.length) {
// 分别比较两个数组的两个元素
if (s1[p1] < s2[p2]) {
s[i++] = s1[p1++];
} else {
s[i++] = s2[p2++];
}
}
//复制剩余元素
//方法1
int length=Math.abs(s1.length-s2.length)+1;
if (p1 < s1.length) {
System.arraycopy(s1, p1, s, i, length);
} else if (p2 < s2.length) {
System.arraycopy(s2, p2, s, i, length);
}
//方法2
// if (p1 < s1.length) {
// System.arraycopy(s1, p1, s, i, s1.length-p1);
// } else if (p2 < s2.length) {
// System.arraycopy(s2, p2, s, i, s2.length-p2);
// }
//方法3
// if (p1 < s1.length) {
// System.arraycopy(s1, p1, s, i, s1.length-s2.length+1);
// } else if (p2 < s2.length) {
// System.arraycopy(s2, p2, s, i, s2.length-s1.length+1);
// }
System.out.println(Arrays.toString(s1));
System.out.println(Arrays.toString(s2));
System.out.println(Arrays.toString(s));
}
}