合并已排序的数组(java)

来源:互联网 时间:1970-01-01

 int[] a = { 1, 3, 5, 7, 9,11 ,12}; int[] b = { 2, 3, 4, 6, 8, 10,33 }; int[] c=new int[a.length+b.length]; int temp=0; int aindex = 0; int bindex = 0; while (aindex < a.length && bindex < b.length) { if (a[aindex] == b[bindex]) { System.out.print(a[aindex]+" "+b[bindex]+" "); c[temp++]=a[aindex]; c[temp++]=b[bindex]; aindex++; bindex++; } else if (a[aindex] < b[bindex]) { System.out.print(a[aindex]+" "); c[temp++]=a[aindex]; aindex++; } else { System.out.print(b[bindex] + " "); c[temp++]=b[bindex]; bindex++; } } while (aindex<a.length){ System.out.print(a[aindex] + " "); c[temp++]=a[aindex]; aindex++; } while (bindex<b.length){ System.out.print(b[aindex] + " "); c[temp++]=b[bindex]; bindex++; } System.out.println("/n"+Arrays.toString(c));



相关阅读:
Top