Java 如何绕过IndexOutOfBounds错误
所以我有一个2D字符数组,我正在使用它。每个空格可以由“C”、“M”或“N”(表示不重要)占据。我有两种方法,一种是检查元素周围的4个空格,另一种是检查元素周围的8个空格。这是前者Java 如何绕过IndexOutOfBounds错误,java,multidimensional-array,indexoutofboundsexception,Java,Multidimensional Array,Indexoutofboundsexception,所以我有一个2D字符数组,我正在使用它。每个空格可以由“C”、“M”或“N”(表示不重要)占据。我有两种方法,一种是检查元素周围的4个空格,另一种是检查元素周围的8个空格。这是前者 public boolean checkAnySpace4(char[][] grid, int x, int y) { if(grid[x-1][y] == 'N' || grid[x-1][y] == 'M'|| grid[x-1][y] == 'C') {
public boolean checkAnySpace4(char[][] grid, int x, int y)
{
if(grid[x-1][y] == 'N' || grid[x-1][y] == 'M'|| grid[x-1][y] == 'C')
{
return false;
}
if(grid[x-1][y-1] == 'N' || grid[x-1][y-1] == 'M'|| grid[x][y-1] == 'C')
{
return false;
}
if(grid[x][y-1] == 'N' || grid[x][y-1] == 'M'|| grid[x][y-1] == 'C')
{
return false;
}
if(grid[x+1][y] == 'N' || grid[x+1][y] == 'M'|| grid[x+1][y] == 'C')
{
return false;
}
else
return true;
}
如果周围的任何元素(4或8)已经被占用,我只希望该方法返回false,如果所有元素都打开,则返回true。我该如何做才能不出现IndexOutOfBounds错误
编辑:下面是代码的其余部分
public void start(int steps)
{
char [][] grid = new char[25][25];
int numCells = 0;
while (numCells < 10) {
//System.out.println("step = " + i);
int random = (int) Math.floor((Math.random() * 24));
int random2 = (int) Math.floor((Math.random() * 24));
if(checkGrid(grid, random, random2) == true) //if current cell is empty
{
addCellToGrid(grid, random, random2, 'N');//add cell with different coordinates
numCells++;
}
}
printGrid(grid);
System.out.print("exiting before steps");
//System.exit(-1);
/*
* Fix array index out of bounds error
*
*/
for(int i = 0; i < steps; i++)//populate array
{
for(int j = 0; j < 25; j++)
{
for(int k = 0; k < 25; k++)
{
if(new Random().nextDouble() <= (1-e)) //probability that cell dies
{
die(grid, j, k);
continue;
}
else ////if not dead, mutate with prob p_n for N,M cells, p_c for C cells
{
if(new Random().nextDouble() <= m) //mutate with prob m
{
mutate(grid, j,k);//mutate cell
if(grid[j][k] == 'N' || grid[j][k] == 'M')//if cell is type N or M
{
if(checkAnySpace4(grid, j, k))//if cell has space to divide
{
if(new Random().nextDouble() <= p_n)//divide with probability p_n
{
divide(grid, j, k);//divide cell
}
}
else
{
continue; //cell does not divide, do nothing
}
}
}
if(grid[j][k] == 'C')//C grid[j][k]s cannot mutate
{
if(checkAnySpace8(grid, j, k))//if cell has space to divide
{
if(new Random().nextDouble() <= p_c)//divide with probability p_c
{
divide(grid, j, k);//divide cell
}
}
}
}
}
}
}
printGrid(grid);
}
public void开始(整数步)
{
字符[][]网格=新字符[25][25];
int numCells=0;
而(numCells<10){
//System.out.println(“步骤=”+i);
int random=(int)Math.floor((Math.random()*24));
int random2=(int)Math.floor((Math.random()*24));
if(checkGrid(grid,random,random2)==true)//如果当前单元格为空
{
addCellToGrid(网格,随机,随机2,'N');//添加具有不同坐标的单元格
numCells++;
}
}
打印网格(网格);
系统输出打印(“步骤前退出”);
//系统退出(-1);
/*
*修复数组索引越界错误
*
*/
对于(int i=0;i if(new Random().nextDouble()向函数中再添加两个变量:size\u x
和size\u y
然后:
您可能需要从size_x和size_y中减去1。您显示的代码没有任何错误检查,以验证传递给该方法的x,y值是否在网格[]中当前有效索引的范围内。您可能希望在此方法中执行一些错误检查,并向我们显示您在何处调用该方法的代码以及网格[]是如何填充的。我已编辑了此帖子,以包含其余相关代码。
public boolean checkAnySpace4(char[][] grid, int x, int y, int size_x, int size_y)
{
if(x == 0 || y == 0 || x == size_x || y == size_y){
/*complain on the console or raise an exception*/
return false;
}
if(grid[x-1][y] == 'N' || grid[x-1][y] == 'M'|| grid[x-1][y] == 'C')
{
return false;
}
if(grid[x-1][y-1] == 'N' || grid[x-1][y-1] == 'M'|| grid[x][y-1] == 'C')
{
return false;
}
if(grid[x][y-1] == 'N' || grid[x][y-1] == 'M'|| grid[x][y-1] == 'C')
{
return false;
}
if(grid[x+1][y] == 'N' || grid[x+1][y] == 'M'|| grid[x+1][y] == 'C')
{
return false;
}
else
return true;
}