简体   繁体   English

为什么我在这里得到 java.lang.ArrayIndexOutOfBoundsException ?

[英]Why do I get java.lang.ArrayIndexOutOfBoundsException here?

public long process(long[][] theArray) {

    long result = 0l;
    int xDimension = 0;
    int yDimension = 0;

    for (int i = 0; i < theArray.length; i++) {
        for (int j = 0; j < theArray[0].length; j++) {
            if (((yDimension + 12) < theArray.length) && ((xDimension + 12) < theArray[0].length)) {
                result = (theArray[yDimension][xDimension + 1]) + (theArray[yDimension][xDimension + 2])
                        + (theArray[yDimension + 1][xDimension]) + (theArray[yDimension + 1][xDimension + 3])
                        + (theArray[yDimension + 2][xDimension]) + (theArray[yDimension + 2][xDimension + 3])
                        + (theArray[yDimension + 3][xDimension + 1]) + (theArray[yDimension + 3][xDimension + 2]);
            }
            xDimension++;
        }
        xDimension -= (theArray[0].length);
        yDimension++;
    }

    return result;

}// method()

--> my console says: Index 5 out of bounds for length 5. But how is that possible together with my if-condition? --> 我的控制台说:索引 5 超出了长度 5 的范围。但是这与我的 if 条件一起怎么可能呢?

xDimension starts with 0 and will be negative after one iteration of the outer loop. xDimension0开始,在外循环的一次迭代后将为负数。

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

相关问题 为什么会收到“ java.lang.ArrayIndexOutOfBoundsException:0” - Why do I get an “java.lang.ArrayIndexOutOfBoundsException: 0” 如何摆脱此java.lang.ArrayIndexOutOfBoundsException错误? - How do i get rid of this java.lang.ArrayIndexOutOfBoundsException error? 为什么在线程“ AWT-EventQueue-0” java.lang.ArrayIndexOutOfBoundsException:2中得到异常? - Why do I get Exception in thread “AWT-EventQueue-0” java.lang.ArrayIndexOutOfBoundsException: 2? 为什么会出现java.lang.ArrayIndexOutOfBoundsException异常? - Why am I getting java.lang.ArrayIndexOutOfBoundsException exception ? 为什么我得到java.lang.ArrayIndexOutOfBoundsException - Why am I getting java.lang.ArrayIndexOutOfBoundsException java.lang.ArrayIndexOutOfBoundsException 为什么我得到这个 - java.lang.ArrayIndexOutOfBoundsException why am i getting this 是什么导致了 java.lang.ArrayIndexOutOfBoundsException 异常,我该如何预防? - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? java.lang.ArrayIndexOutOfBoundsException:0 - java.lang.ArrayIndexOutOfBoundsException:0 java.lang.ArrayIndexOutOfBoundsException:2&gt; = 2 - java.lang.ArrayIndexOutOfBoundsException: 2 >= 2 java.lang.ArrayIndexOutOfBoundsException:2&gt; = 0 - java.lang.ArrayIndexOutOfBoundsException: 2 >= 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM