线程“AWT-EventQueue-0”中的异常java.lang.ArrayIndexOutOfBoundsException:10

时间:2023-01-26 22:06:43

I'm making a minesweeper using a recursive method to open all tiles adjacent to the block "0".

我正在使用递归方法打开扫雷,打开与块“0”相邻的所有切片。

Everything goes well until I get the exception I mentioned in the title. The exception is fired at if(removalList[num1][num2] == 1){return;}, but made sure to set all the initial values in the removal list to zero. (For your reference, 1 means that the item has already been added to the removalList for later removal).

一切顺利,直到我得到标题中提到的例外。 if(removeList [num1] [num2] == 1){return;}触发异常,但确保将删除列表中的所有初始值设置为零。 (供您参考,1表示该项已添加到removeList中以便以后删除)。

I also checked to see if it was in bounds by doing if(num1 > gameWidth || num2 > gameHeight || num1 < 0 || num2 < 0){return;}. (gameHeight and width are both 10), but for some reason it thinks that it's out of bounds.

我还通过执行if(num1> gameWidth || num2> gameHeight || num1 <0 || num2 <0){return;}来检查它是否在边界内。 (gameHeight和width都是10),但由于某种原因,它认为它超出了界限。

Thanks for your help!

谢谢你的帮助!

private void function(int c5, int r5)
{
    int num1 = c5;
    int num2 = r5;

    if(num1 > gameWidth || num2 > gameHeight || num1 < 0 || num2 < 0)
    {
        return;
    }
    if(removalList[num1][num2] == 1)
    {
        return;
    }
    if(blocks[num1][num2] == 0)
    {       
        System.out.println("Added (" + num1 + ", " + num2 + ") to removal list.");
        removalList[num1][num2] = 1;

        function(num1-1, num2);
        function(num1, num2-1);
        function(num1+1, num2);
        function(num1, num2+1);

    }
    else if(blocks[num1][num2] > 0 && blocks[num1][num2] < 9)
    {
        removalList[num1][num2] = 1;
        return;
    }
    else
    {
        return;
    }
}

2 个解决方案

#1


1  

Without seeing further code, in particular the declaration of removalList, I can only guess. My guess is, that removalList has gameWidth * gameHeight elements. So the indices run from 0 to gameWidth - 1 and from 0 to gameHeight - 1. Your check allows indices of up to gameWidth and gameHeight, which would cause the exception you are getting.

没有看到进一步的代码,特别是removeList的声明,我只能猜测。我的猜测是,removeList有gameWidth * gameHeight元素。所以索引从0运行到gameWidth - 1,从0运行到gameHeight - 1.你的检查允许最多gameWidth和gameHeight的索引,这将导致你得到的异常。

#2


1  

If the size of an array is 10, the max possible accessible index in the array would be array[size-1]. If you try to access an index, greater than or equal to the size, then you'd get what is called an ArrayIndexOutOfBoundsException.

如果数组的大小为10,则数组中可能的最大可访问索引将为array [size-1]。如果您尝试访问大于或等于大小的索引,那么您将获得所谓的ArrayIndexOutOfBoundsException。

E.g:-

int[] test = new int[5];
test[0] = 1; // Allowed
test[1] = 2; // Allowed
test[2] = 3; // Allowed
test[3] = 4; // Allowed
test[4] = 5; // Allowed
test[5] = 6; // NOT Allowed - You'll get the ArrayIndexOutOfBoundsException here.

Hence, in your case,

因此,在你的情况下,

removalList[9][9] is allowed, but removalList[10][10] will give the ArrayIndexOutOfBoundsException.

removeList [9] [9]是允许的,但removeList [10] [10]将给出ArrayIndexOutOfBoundsException。

#1


1  

Without seeing further code, in particular the declaration of removalList, I can only guess. My guess is, that removalList has gameWidth * gameHeight elements. So the indices run from 0 to gameWidth - 1 and from 0 to gameHeight - 1. Your check allows indices of up to gameWidth and gameHeight, which would cause the exception you are getting.

没有看到进一步的代码,特别是removeList的声明,我只能猜测。我的猜测是,removeList有gameWidth * gameHeight元素。所以索引从0运行到gameWidth - 1,从0运行到gameHeight - 1.你的检查允许最多gameWidth和gameHeight的索引,这将导致你得到的异常。

#2


1  

If the size of an array is 10, the max possible accessible index in the array would be array[size-1]. If you try to access an index, greater than or equal to the size, then you'd get what is called an ArrayIndexOutOfBoundsException.

如果数组的大小为10,则数组中可能的最大可访问索引将为array [size-1]。如果您尝试访问大于或等于大小的索引,那么您将获得所谓的ArrayIndexOutOfBoundsException。

E.g:-

int[] test = new int[5];
test[0] = 1; // Allowed
test[1] = 2; // Allowed
test[2] = 3; // Allowed
test[3] = 4; // Allowed
test[4] = 5; // Allowed
test[5] = 6; // NOT Allowed - You'll get the ArrayIndexOutOfBoundsException here.

Hence, in your case,

因此,在你的情况下,

removalList[9][9] is allowed, but removalList[10][10] will give the ArrayIndexOutOfBoundsException.

removeList [9] [9]是允许的,但removeList [10] [10]将给出ArrayIndexOutOfBoundsException。