# Testing values in an array.

• September 27th, 2011, 10:13 PM
bholzer
Testing values in an array.
Okay, I'm writing a program for class. It creates a random boolean array of a given size, then loops through the array and on each value, it tests all the values around it. If it has a certain number of 'true' around it, it executes some code. My issue is it starts at the top left of the array, and the program tests values that are outside of that array. How can I remedy this?

Here is the code:
Code :

```for (int t = 0; t < N; t++) for (int j = 0; j < N; j++) { if (isLive[t][j] == true) {   if (isLive[t][j+1]==true) livecount ++; if (isLive[t][j-1]==true) livecount ++; if (isLive[t+1][j+1]==true) livecount ++; if (isLive[t-1][j+1]==true) livecount ++; if (isLive[t-1][j-1]==true) livecount ++; if (isLive[t+1][j]==true) livecount ++; if (isLive[t-1][j]==true) livecount ++; if (isLive[t+1][j-1]==true) livecount ++; if (livecount == 1) isLive[t][j] = false; if (livecount > 3) isLive[t][j] = false;   } else { if (isLive[t][j+1]==true) livecount ++; if (isLive[t][j-1]==true) livecount ++; if (isLive[t+1][j+1]==true) livecount ++; if (isLive[t-1][j+1]==true) livecount ++; if (isLive[t-1][j-1]==true) livecount ++; if (isLive[t+1][j]==true) livecount ++; if (isLive[t-1][j]==true) livecount ++; if (isLive[t+1][j-1]==true) livecount ++; if (livecount == 3) isLive[t][j] = true; }   }```
• September 28th, 2011, 03:58 AM
Sean4u
Re: Testing values in an array.
One way to do it would be to catch the ArrayIndexOutOfBoundsException. Another would be to use if statements to test your array indexes before you use them. Yet another would be to replace the booleans at each point in the array with an object that 'knows' which of its neighbours are present (I do this in some compute-intensive code for an array-of-arrays-of-arrays where some neighbours may be missing). Yet another - if your array of arrays is guaranteed to be rectangular - is to consider 'wrapping' in your field using the modulo operator on indexes, so that the index 'before' 0 is the last index in your array. That last option is only usually appropriate in some applications - you wouldn't use it to model a Petri dish for example, although you might to model the surface of a globe.

There might be a few more options - if those options are not suitable, explain why and maybe someone will come up with something else.
• September 28th, 2011, 08:12 AM
Norm
Re: Testing values in an array.
Quote:

program tests values that are outside of that array. How can I remedy this?
Arrays have a length attribute that you can use to determine the size/max index of the array.
• September 28th, 2011, 02:44 PM
bholzer
Re: Testing values in an array.
I plan to use if statements to test whether the current value is on an edge or corner. If it is, I need certain statements to not execute, but now I'm stuck there!!
• September 28th, 2011, 02:47 PM
Norm
Re: Testing values in an array.
The first element in an array is at element 0.
The last element in an array is at array.length-1
• September 28th, 2011, 03:16 PM
bholzer
Re: Testing values in an array.
Well, I got it to work, but it might just be the ugliest code I've ever written:

Code :

```public static boolean[][] life(boolean[][] isLive, int N) { int livecount = 0; for (int t = 0; t < N; t++) for (int j = 0; j < N; j++) { if (isLive[t][j] == true) { if (t == 0) { if (isLive[t][j+1]==true) livecount ++; if (isLive[t][j-1]==true) livecount ++; if (isLive[t+1][j+1]==true) livecount ++; if (isLive[t+1][j]==true) livecount ++; if (isLive[t+1][j-1]==true) livecount ++; }   if (t == N) { if (isLive[t][j+1]==true) livecount ++; if (isLive[t][j-1]==true) livecount ++; if (isLive[t-1][j+1]==true) livecount ++; if (isLive[t-1][j-1]==true) livecount ++; if (isLive[t-1][j]==true) livecount ++; }   if (j == 0) { if (isLive[t][j+1]==true) livecount ++; if (isLive[t+1][j+1]==true) livecount ++; if (isLive[t-1][j+1]==true) livecount ++; if (isLive[t+1][j]==true) livecount ++; if (isLive[t-1][j]==true) livecount ++; }   if (j == N) { if (isLive[t][j-1]==true) livecount ++; if (isLive[t-1][j-1]==true) livecount ++; if (isLive[t+1][j]==true) livecount ++; if (isLive[t-1][j]==true) livecount ++; if (isLive[t+1][j-1]==true) livecount ++; } if (isLive[t][j+1]==true) livecount ++; if (isLive[t][j-1]==true) livecount ++; if (isLive[t+1][j+1]==true) livecount ++; if (isLive[t-1][j+1]==true) livecount ++; if (isLive[t-1][j-1]==true) livecount ++; if (isLive[t+1][j]==true) livecount ++; if (isLive[t-1][j]==true) livecount ++; if (isLive[t+1][j-1]==true) livecount ++; if (livecount == 1) isLive[t][j] = false; if (livecount > 3) isLive[t][j] = false; livecount = 0;   } else {   if (t == 0) { if (isLive[t][j+1]==true) livecount ++; if (isLive[t][j-1]==true) livecount ++; if (isLive[t+1][j+1]==true) livecount ++; if (isLive[t+1][j]==true) livecount ++; if (isLive[t+1][j-1]==true) livecount ++; }   if (t == N) { if (isLive[t][j+1]==true) livecount ++; if (isLive[t][j-1]==true) livecount ++; if (isLive[t-1][j+1]==true) livecount ++; if (isLive[t-1][j-1]==true) livecount ++; if (isLive[t-1][j]==true) livecount ++; }   if (j == 0) { if (isLive[t][j+1]==true) livecount ++; if (isLive[t+1][j+1]==true) livecount ++; if (isLive[t-1][j+1]==true) livecount ++; if (isLive[t+1][j]==true) livecount ++; if (isLive[t-1][j]==true) livecount ++; }   if (j == N) { if (isLive[t][j-1]==true) livecount ++; if (isLive[t-1][j-1]==true) livecount ++; if (isLive[t+1][j]==true) livecount ++; if (isLive[t-1][j]==true) livecount ++; if (isLive[t+1][j-1]==true) livecount ++; } if (isLive[t][j+1]==true) livecount ++; if (isLive[t][j-1]==true) livecount ++; if (isLive[t+1][j+1]==true) livecount ++; if (isLive[t-1][j+1]==true) livecount ++; if (isLive[t-1][j-1]==true) livecount ++; if (isLive[t+1][j]==true) livecount ++; if (isLive[t-1][j]==true) livecount ++; if (isLive[t+1][j-1]==true) livecount ++; if (livecount == 1) isLive[t][j] = false; if (livecount > 3) isLive[t][j] = false; livecount = 0;   }   } return isLive; }```
• September 28th, 2011, 03:37 PM
Norm
Re: Testing values in an array.
Yes, uncommented code can be very ugly.
• September 28th, 2011, 05:18 PM
Sean4u
Re: Testing values in an array.
Are you sure that works? It looks to me as though it could use a few more 'else ifs'. Are you implementing Conway's Game Of Life? One sure way to check if your implementation is correct is to set the cells up with a Glider pattern and check that it flies correctly:
The glider from john conway&#39;s game of life - YouTube