基本上,有两个单独的预排序数组,你需要将它们合并并排序(当然不能使用sort()方法)。以下是我的代码:
public static void main(String[] args) {
int a [] = {3,5,7,9,12,14, 15};
int b [] = {6 ,7, 10};
int j = 0;
//output array should be 3,5,6,7,7,9,10,12,14,15
int c [] = new int[a.length+b.length];//10 values
for (int i = 0;i<b.length;i++){
while(b[i]>a[j]){
c[j] = a[j] ;
j++;
}
if(b[i] == a[j]){
c[j] = b[i];
c[j+1] = a[j];
}
c[j] = b[i];
j++;
}
for(int i = 0;i<c.length;i++)
System.out.println(c[i]);
}
我猜测我得到的零是由于布尔值中的错误(< & >),但我似乎无法弄清楚。前四个是正常的,但一旦到达重复的7,它就变得疯狂了。
请帮助我理解,而不只是更改代码。