public class JoinArray {
public static void main(String[] args) {
int[] a1 = {1, 3, 5, 7, 13};
int[] a2 = {2, 4, 6, 12, 14};
System.out.println(Arrays.toString(sort(a1, a2)));
}
public static int[] sort(int[] a, int[] b){
int[] c = new int[a.length + b.length];
int i = 0, j = 0, k = 0;
while (i < a.length && j < b.length){
if (a[i] >= b[j]){
c[k++] = b[j++];
}else{
c[k++] = a[i++];
}
}
while (j < b.length){
c[k++] = b[j++];
}
while (i < a.length){
c[k++] = a[i++];
}
return c;
}
}
以上只是一种常规思路,当然你也可以先将两个数组进行合并,合并之后再进行排序输出。
0 Comments