`
gongxihai
  • 浏览: 900 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

合并已排序的数组(java)

    博客分类:
  • java
阅读更多
  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));

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics