Reputation: 37
What i want to do in this conditional is make sure the object in the array (neighbors of the middle cell) is physically inside the array, so i thought i would just make the third variable in the constructor (age) equal to zero, thinking if it cant find that instance variable it wouldn't go further. but it gives me an out of bounds -1 exception and i don't know how to re write it to avoid that. (I apologize if this isnt enough detail, i can provide more, just ask)
So heres the part of my code im stuck on:
for (int x = 0; x < Grid.columns; x++) {
for (int y = 0; y < Grid.rows; y++) {
int nCount = 0;
int hCount = 0;
//check neighbors
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
//check if valid and identity
//STUCK HERE---> if(i !=0 && j !=0)
if (board[x + i][y + j].age == 0) {
nCount++;
if (board[x + i][y + j].getPreviousValue() == 0)
hCount++;
}
}
}
board[x][y].setCurrentValue(hCount / nCount, (nCount - hCount) / nCount);
}
}
Upvotes: 1
Views: 1456
Reputation: 17137
Your problem are the 'edge-cells' of your grid, since you access indices outside the array. (like -1 and array.length). For a sound solution you would have to check for this conditions.
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int neighbor_x = x + i;
int neighbor_y = y + j;
if (neighbor_x < 0 || neighbor_x >= board.length) {
// Out of Grid
}
if (neighbor_y < 0 || neighbor_y >= board[neighbor_x].length) {
// Out of Grid
}
if (board[neighbor_x][neighbor_y].age == 0) {
nCount++;
if (board[x + i][y + j].getPreviousValue() == 0)
hCount++;
}
}
}
Another possibility would be to simply catch the exception. However, this can be considered bad style. I would not recommend using this code:
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
try {
if (board[x + i][y + j].age == 0) {
nCount++;
if (board[x + i][y + j].getPreviousValue() == 0)
hCount++;
}
}
} catch (IndexOutOfBoundsException e) {
// Here you know that the exception occured.
//
// In this particular case we will not write any code,
// since the proper handling of this exception is to move
// on to the next cell.
}
}
}
And as a remark: style is always subjective, but 6 levels of nesting is usually not good.
Upvotes: 3
Reputation: 2184
with all this complicated code that i feel i cant understand what goes where, what i understand is that you want to deal with an object in an array in the second dimension, you can do something like this :
int[][][] ints = {
{{1,2,3},{4,5,6}}, // dimension 0
{{},{},{7,8,9}} // dimension 1
};
for(int i = 0;i<ints.length;i++){
for(int t = 0;t<ints[i].length;t++){
// check if the middle Dimension array is initialized
if((ints[i][t] != null) && ints[i][t].length > 0){
for(int z = 0;z<ints[i][t].length;z++){
System.out.println("Dimension "+i+"-"+t+" : "+ints[i][t][z]);
}
}
}
}
/* Result
Dimension 0-0 : 1
Dimension 0-0 : 2
Dimension 0-0 : 3
Dimension 0-1 : 4
Dimension 0-1 : 5
Dimension 0-1 : 6
Dimension 1-2 : 7 // skipped the empty dimentions 1-0,1-1
Dimension 1-2 : 8
Dimension 1-2 : 9
*/
but a piece of advice, i think that starting your loop with -1 is the cause of this ... please try to start your for loop with "0" ... your code is trying to find an element of index -1 in the array, which causes the Exception
you can catch this Exception and "eat/skip" it, but this is a bad idea
Upvotes: 0
Reputation: 1243
My suggestion would be to do some bounds checking.
Using your example, on the very first time inside the fourth(!) for loop, you have x=0
, y=0
, i=-1
, and j=-1
. This means you're trying to access board[-1][-1]
, which obviously doesn't exist. One (not necessarily optimal) way to fix this would be to use another if
statement that checks for out-of-bounds conditions, such as if ((x+i) >= 0) && ((x+1) < Grid.columns) && ((y+j) >= 0 && (y+j) < Grid.Rows)
.
The problem is not with the if(i !=0 && j !=0)
part.. it's with the fact that your indexes go out of bounds.
And as an aside, as has been mentioned in the comments and other answers, having a fourth nested for
loop is not a great idea, and can easily lead to memory or run-time performance problems. In this particular case, since you're checking at most eight points (four if you uncomment the !=0
if statement) with your third and fourth nested for loops, you could try actually enumerating just those eight (or four).
And as another aside, if you have nested if
statements, like so:
if(test1)
if(test2)
doSomething();
then you can try combining them into a single if statement:
if(test1 && test2)
doSomething();
This won't always work (else
statements come to mind), but it can help to make your code readable.
Upvotes: 1
Reputation: 23483
You can't have array's with a negative value, in your case -1. Arrays start at 0, so with i
equaling -1 & j
equaling -1, you are getting that error. You are adding -1 to 0, thus getting -1 for for your array selection. Keep the numbers positive.
I'm also not sure what you are trying to do here. I mean where else would the element be if it isn't in the array? If you are wondering if the item is in the array, there are array methods you can use to see if a particular value is in an array. If you are trying to determine if a particular value exists, then that's one things, I'm just not clear on what you are trying to do.
Upvotes: 2