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]+",");
}
output: 4,24,7,53,2,6,9,19,12,41,35,71,8,1,49,68,0,0,0
how can i print out the rest of elements of queue2.?
Your merge algorithm makes an assumption that
queue1.length < queue2.length. Although it is correct for your program, it is not generally a good thing to make such assumptions.Change the merge algorithm to go through both arrays until you hit the length of the shorter one, then dump the remaining elements of the longer array into the tail of the merged array. You can do it all in a single loop, like this: