Reputation: 135
Hello I am writing a program for a programming class and I am getting a:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 18
at Life.countNeighbors(Life.java:200)
at Life.genNextGrid(Life.java:160)
I've gotten ArrayIndexOutOfBoundsException
errors before, usually caused when I try to add or substract a index of an array. However this time it is quite different and I was hoping someone here can point out why the error is occuring.
info about my program: the program is like John Conway game of life. using a 2d array and then setting certain elements to (true = alive) or (false = dead) the program then determine if a cell lives or dies in the next generation base on the number of neighbors it has. (3 neighbors = birth of a new cell) (2,3 neighbor = they stay alive) anything else they died in the next generation.
The IndexOutOfBound error is caused by this line according to my editor:
if(!(grid[1][1]) && !(grid[1][18]) && !(grid[18][1]) && !(grid[18][18]))
I created the above line as a constraint, and it should not tell java to search a index beyond the bounds of the original array since they are merely booleans (true/false)statement in the end. If anyone can help me debug this error that would be splendid.
HERE IS MY CODE: (does not include main method)
public static void clearGrid ( boolean[][] grid )
{
int col;
int row = 1;
while(row < 18){
for(col = 1; col < 18; col++){
grid[row][col]= false;//set each row to false
}
row++;
}//set all elements in array to false
}
public static void genNextGrid ( boolean[][] grid )
{
//new tempprary grid
boolean[][] TempGrid = new boolean[GRIDSIZE][GRIDSIZE];
TempGrid= grid; // copy the current grid to a temporary grid
int row = 1;
int col = 1;
countNeighbors(TempGrid, row, col); // passes the TempGrid to countNieghbor method
for(row = 1; row < 18; row++){
countNeighbors(TempGrid, row, col);
for(col = 1; col < 18; col++){
countNeighbors(TempGrid, row, col);
if(countNeighbors(grid, row, col) == 3)
{
TempGrid[row][col] = true;
}
else if(countNeighbors(grid, row, col) == 2 || countNeighbors(grid, row, col) == 3)
{
TempGrid[row][col] = true;
}
else
{
TempGrid[row][col] = false;
}
}
}
}
public static int countNeighbors ( final boolean[][] grid, final int row, final int col )
{
int n = 0; //int used to store the # of neighbors
int temprow = row;
int tempcol = col;
//count # of neighbors for the cell on the edge but not the corner
for(temprow = row; temprow <= 18; temprow++)
{
for(tempcol = row; tempcol <= 18; tempcol++)
{
if(temprow == 1 || temprow == 18 || tempcol == 1 || tempcol ==18)
{
if(!(grid[1][1]) && !(grid[1][18]) && !(grid[18][1]) && !(grid[18][18]))
{
if(grid[temprow][tempcol] == true)
{
n++;
}
}
}
}
}
//count # of neighbors for the corner cells
for(temprow = row; temprow <= 18; temprow++)
{
for(tempcol = row; tempcol <= 18; tempcol++)
{
if(grid[1][1] || grid[1][18] || grid[18][1] || grid[18][18])
{
if(grid[temprow][tempcol] == true)
{
n++;
}
}
}
}
// count the cells that are not on the edge or corner
while(temprow >= 2 && tempcol >= 2 && temprow <= 17 && tempcol <= 17)
{
for(temprow = row; temprow-1 <= temprow+1; temprow++)
{
for(tempcol = col; tempcol-1 <= tempcol+1; tempcol++)
{
if(grid[temprow][tempcol] == true)
{
n++;
}
}
}
}
return n; // return the number of neighbors
}
Upvotes: 0
Views: 417
Reputation: 500437
In Java, array indices are numbered from 0
to n-1
. From looking at your code, it would appear that it assumes that they are numbered from 1
to n
.
Upvotes: 5
Reputation: 24316
Without a full stack trace and an indication as to where the problem lies, this is my best guess:
grid[18][1]
The value 18
is beyond the size of the array you can access, in Java arrays are zero based (0)
. Since I have seen 17
all throughout your post, this seems like the most logical reason why.
Upvotes: 7