简体   繁体   English

我正在尝试合并两个排序的 arrays 但得到 java.lang.ArrayIndexOutOfBoundsException: 5 无法弄清楚为什么

[英]I'm trying to merge two sorted arrays but getting java.lang.ArrayIndexOutOfBoundsException: 5 can't figure out why

It is going out bounds on the first if statement even after I'm checking the values of j and k before incrementing即使在我在递增之前检查 j 和 k 的值之后,它也会超出第一个 if 语句的范围

`int[] Merge(int[]a,int[]b){
int n=a.length+b.length;
int[] sol= new int[n];
int i=0,j=0,k=0;

while(i<n){

if(a[j]<b[k]){

    sol[i]=a[j];

    if(j<a.length)j++;
}
else if(b[k]<a[j]){

        sol[i]=b[k];

        if(k<b.length)k++;

}

i++;

}


return sol;

}`

I can't understand where the array is going out of bounds我不明白数组在哪里越界

Consider the case when either j=a.length-1 or k=b.length-1 then your if condition [ if(j<a.length)j++ ;考虑当 j=a.length-1 或 k=b.length-1 然后你的 if 条件 [ if(j<a.length)j++ ; or if(k<b.length)k++;if(k<b.length)k++; ]passes and makes j==a.length to true or k==b.length to true and the while loop access the array again when it throws ArrayIndexOutofBound exception. ] 传递并使 j==a.length 为 true 或 k==b.length 为 true 并且当 while 循环抛出 ArrayIndexOutofBound 异常时再次访问该数组。 Also your code doesn't handle common elements in the array.此外,您的代码不处理数组中的常见元素。 Please check the correct code below请检查下面的正确代码

int[] Merge(int[]a,int[]b){
int n=a.length+b.length;
        int[] sol= new int[n];
        int i=0,j=0,k=0;

        while(i<n){
            if(a[j]<=b[k]){ // equality check handles common case

                sol[i++]=a[j++];
                if(j==a.length)
                   break;

            }
            else if(b[k]<a[j]){
                sol[i++]=b[k++];
                if(k==b.length)
                    break ;

            }


        }
        if(j==a.length) {
            while (k != b.length)
                sol[i++]=b[k++];
        }
        if(k==b.length) {
            while (j != a.length)
                sol[i++]=a[j++];
        }
        return sol;
}

The problem is here:问题在这里:

if(j<a.length)j++;

Here when j = a.length - 1 then it will become a.length because of j++.这里当 j = a.length - 1 时,由于 j++,它将变为 a.length。 Now, a[j] will be out of bounds because j = a.length.现在,a[j] 将超出范围,因为 j = a.length。 a[a.length] is always out bounds. a[a.length] 总是超出范围。 The indexing starts from 0, so last index is a.length - 1.索引从 0 开始,所以最后一个索引是 a.length - 1。

暂无
暂无

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

相关问题 在线程“main”中出现异常 java.lang.ArrayIndexOutOfBoundsException 错误,并试图找出解决方案 - Getting a Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException error, and am stuck trying to figure out a solution java.lang.ArrayIndexOutOfBoundsException:0-我无法弄清为什么代码超出范围 - java.lang.ArrayIndexOutOfBoundsException: 0 - I cant figure out why the code is running out of bounds 为什么会出现java.lang.ArrayIndexOutOfBoundsException异常? - Why am I getting java.lang.ArrayIndexOutOfBoundsException exception ? 无法弄清楚为什么我得到了java.lang.NullPointerExceptio - Can't figure out why I'm getting a java.lang.NullPointerExceptio 为什么我得到java.lang.ArrayIndexOutOfBoundsException - Why am I getting java.lang.ArrayIndexOutOfBoundsException java.lang.ArrayIndexOutOfBoundsException 为什么我得到这个 - java.lang.ArrayIndexOutOfBoundsException why am i getting this 我不断收到java.lang.arrayindexoutofboundsexception 5错误,但找不到该错误 - I keep getting a java.lang.arrayindexoutofboundsexception 5 error, but I can't find the error 我无法解决此问题:java.lang.ArrayIndexOutOfBoundsException - I can't fix this: java.lang.ArrayIndexOutOfBoundsException 无法追踪到java.lang.ArrayIndexOutOfBoundsException: - Can't trace an java.lang.ArrayIndexOutOfBoundsException: 试用多维数组,在线程“ main”中接收异常java.lang.ArrayIndexOutOfBoundsException - Trying out Multidimensional Arrays, recieveing Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM