线程“AWT-EventQueue-0”中的异常 java.lang.ArrayIndexOutOfBoundsException: 10
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16161959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 10
提问by user2052855
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(removalList[num1][num2] == 1){return;}
,但确保将删除列表中的所有初始值设置为零。(供您参考,这1
意味着该项目已添加到removalList
稍后删除)。
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 and width are both 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;
}
}
回答by SudoRahul
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.
removalList[9][9]
是允许的,但removalList[10][10]
会给出ArrayIndexOutOfBoundsException。
回答by Philipp Matthias Sch?fer
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.
没有看到进一步的代码,特别是 的声明removalList
,我只能猜测。我的猜测是,这removalList
有gameWidth * gameHeight
元素。所以索引从0
togameWidth - 1
和 from 0
to运行gameHeight - 1
。您的检查允许索引最多gameWidth
和gameHeight
,这会导致您获得异常。