2
    int [] queue1 = {4,7,2,9,12,35,8,49};
    int [] queue2 = {24,53,6,19,41,71,1,68,11,32,99}        
    int[]mergeQ = new int[queue1.length + queue2.length];

    for(int i=0; i < queue1.length; i++ )
    {

      mergeQ[i*2] = queue1[i];       
      mergeQ[i*2+1] = queue2[i];  
    }
    for(int i=0; i < mergeQ.length; i++) {            
        System.out.print(mergeQ[i]+",");
    }

输出:4,24,7,53,2,6,9,19,12,41,35,71,8,1,49,68,0,0,0

我如何打印出 queue2 的其余元素。?

4

6 回答 6

6

您的合并算法假设queue1.length < queue2.length. 尽管它对您的程序是正确的,但做出这样的假设通常不是一件好事。

更改合并算法以遍历两个数组,直到达到较短数组的长度,然后将较长数组的剩余元素转储到合并数组的尾部。您可以在一个循环中完成所有操作,如下所示:

int p = 0;
for (int i = 0 ; i < queue1.length || i < queue2.length ; i++) {
    if (i < queue1.length) {
        mergeQ[p++] = queue1[i];
    }
    if (i < queue2.length) {
        mergeQ[p++] = queue2[i];
    }
}
于 2012-12-01T13:50:34.043 回答
1

这是一种方法:

    int[] queue1 = { 4, 7, 2, 9, 12, 35, 8, 49 };
    int[] queue2 = { 24, 53, 6, 19, 41, 71, 1, 68, 11, 32, 99 };
    int[] mergeQ = new int[queue1.length + queue2.length];

    int dest = 0;
    int src1 = 0;
    int src2 = 0;
    while (src1 < queue1.length || src2 < queue2.length) {
        if (src1 < queue1.length) {
            mergeQ[dest++] = queue1[src1++];
        }
        if (src2 < queue2.length) {
            mergeQ[dest++] = queue2[src2++];
        }
    }

    for (int i = 0; i < mergeQ.length; i++) {
        System.out.print(mergeQ[i] + ",");
    }

无论是否更短,这都queue1有效queue2

它还保留了算法的属性,即两个队列的元素是交错的。如果这不重要,那么整个事情可以用两次调用来代替System.arraycopy()

于 2012-12-01T13:51:35.073 回答
0

我不确定是否有任何顺序您希望合并两个队列(例如队列 1 中的一个元素被队列 2 中的一个元素闲置),尽管闲置代码会将所有元素放入合并队列中。

int [] 
    queue1 = {4,7,2,9,12,35,8,49},
    queue2 = {24,53,6,19,41,71,1,68,11,32,99},
    mergeQ = new int[queue1.length + queue2.length];

for (int i=0; i < queue1.length; i++ )
    mergeQ[i] = queue1[i];

for (int i = 0; i < queue2.length; i++)
    mergeQ[queue1.length + i] = queue2[i];

for(int i=0; i < mergeQ.length; i++)
    System.out.print(mergeQ[i]+",");
于 2012-12-01T13:50:17.710 回答
0

检查哪个阵列更长。为两者之间的差异保留一个计数器。稍微修改一下代码……有点不正统……但更干净一点

int [] queue2 = {24,53,6,19,41,71,1,68,11,32,99};

int[]mergeQ = new int[queue1.length + queue2.length];

int larger=queue1.length;
int smaller=queue2.length;

if(queue1.length < queue2.length)
  {
   larger=queue2.length;
   smaller=queue1.length;
  }

    for(int i=0; i < queue1.length; i++ )
    {
      mergeQ[i*2] = queue1[i];       
      mergeQ[i*2+1] = queue2[i];

    }

    for(int i=mergeQ.length; i < queue2.length; i++ )
    {
       mergeQ[i] = queue2[larger-smaller];
       smaller++;
    }

    for(int i=0; i < mergeQ.length; i++){


        System.out.print(mergeQ[i]+",");
    }

这应该为你做

于 2012-12-01T13:58:32.133 回答
0

输入:{1, 3, 5, 7} 和 {2, 4, 6} 输出:{1, 2, 3, 4, 5, 6, 7}}

        int c[] = {2, 4, 6, 8};
        int n[] = {1, 3, 5, 7, 9};
        int f = 0;
        int res[] = new int[c.length + n.length];
        for(int i=0;i<(c.length + n.length); i++)
        {
            if(c.length > n.length)
            {
                if(i<c.length)
                    res[f++] = c[i];
                else if(f<=res.length-1)
                    res[f++] = c[i];

                if(i<n.length)
                res[f++] = (char) n[i];
            }else{
                if(i<n.length)
                    res[f++] = n[i];
                else if(f<=res.length-1)
                    res[f++] = n[i];

                if(i<c.length)
                res[f++] = (char) c[i];
            }

        }
        for(int i=0;i<res.length;i++)
        {
            System.out.print("   "+res[i]);
        }
    }
}
于 2014-05-13T13:57:22.760 回答
0
int[] queue1 = {4,7,2,9,12,35,8,49};
int[] queue2 = {24,53,6,19,41,71,1,68,11,32,99}        
int[] mergeQ = new int[queue1.length + queue2.length

int i=0;
for(; i < queue1.length; i++ ){
  mergeQ[i*2] = queue1[i];       
  mergeQ[i*2+1] = queue2[i];
}
if(queue1.length>queue2.length){ 
 for(int j=i;j<queue1.length;j++)
  mergeQ[j+2] = queue1[j];
}else if(queue1.length<queue2.length){
 for(int j=i;j<queue2.length;j++)
  mergeQ[j+2] = queue2[j];
}
于 2012-12-01T13:56:07.340 回答