简体   繁体   English

合并两个数组时出错

[英]Error while merging two arrays

I have two sorted arrays arr1 and arr2 and I am trying to merge these two arrays into another array 'output'. 我有两个排序的数组arr1和arr2,并且我试图将这两个数组合并到另一个数组“输出”中。 But the problem is if the first array gets exhausted ie array arr1 gets completely used then value of 'i' will be more than the length of the array so on next iteration arr[i] gives an error. 但是问题是,如果第一个数组用尽,即数组arr1被完全使用,那么'i'的值将大于数组的长度,因此在下一次迭代中arr [i]会给出错误。 What can be done to avoid this? 如何避免这种情况?

    int i=0;
    int j=0;

    for(int k=0;k<output.length;k++) {      

            if(arr1[i]<arr2[j]) {
                output[k]=arr1[i];
                i++;
            }


            else{           
                   output[k]=arr2[j];
                   j++;         
            }       

    }

You need to check if the arrays are exhausted : 您需要检查数组是否用尽:

int i=0;
int j=0;

for(int k=0;k<output.length;k++) {      
    if(i < arr1.length && j < arr2.length) { 
        // both arrays still have unused elements
        if (arr1[i]<arr2[j]) {
            output[k]=arr1[i];
            i++;
        } else {           
           output[k]=arr2[j];
           j++;         
        } 
    } else if (i < arr1.length) {
        // only the first array has unused elements
        output[k]=arr1[i];
        i++;   
    } else if (j < arr2.length) {
        // only the second array has unused elements
        output[k]=arr2[j];
        j++; 
    } 
}

We can take advantage of the fact that the value of k does not have to be checked since the output array has its length equal to the sum of lengths of arr1 and arr2. 我们可以利用以下事实:不必检查k的值,因为输出数组的长度等于arr1和arr2的长度之和。 Once either of the arrays has its elements exhausted, we exit from the main for loop( hence the conditions i < arr1.length && j < arr2.length). 一旦两个数组中的任何一个元素用尽,我们就从main for循环退出(因此条件i <arr1.length && j <arr2.length)。 Now, since one of the arrays has been exhausted, we just need to dump all of the remaining elements of the other array into the output array. 现在,由于其中一个数组已用尽,我们只需要将另一个数组的所有剩余元素转储到输出数组中。 Hence we check the condition if i != arr1.length which if true would mean that j is equal to arr2.length therefore arr2 has been exhausted and elements of arr1 have to be dumped into output array. 因此,我们检查条件i = arr1.length,如果为true,则意味着j等于arr2.length,因此arr2已耗尽,并且arr1的元素必须转储到输出数组中。 Else, we dump the remaining of the arr2 elements into the output array. 否则,我们将其余的arr2元素转储到输出数组中。

int i=0;
int j=0;

for(int k=0;i<arr1.length && j < arr2.length ;k++) {      

        if(arr1[i]<arr2[j]) {
            output[k]=arr1[i];
            i++;
        }


        else{           
               output[k]=arr2[j];
               j++;         
        }       

}
if(i != arr1.length){
    for(; i < arr1.length; i++,k++)
    output[k]=arr1[i];
}
else{
    for(; j < arr2.length; j++,k++)
    output[k]=arr2[j];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM