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

问题描述 投票:0回答:1
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()

->我的控制台说:索引5超出长度5的范围。但是这与我的if条件一起怎么可能?

java arrays multidimensional-array indexoutofboundsexception
1个回答
0
投票

[xDimension0开头,并且在外循环的一次迭代后将为负。

© www.soinside.com 2019 - 2024. All rights reserved.