Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Testing values in an array.

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    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;
    				}
     
    			}


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Testing values in an array.

    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.

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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

  6. #6
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Testing values in an array.

    Well, I got it to work, but it might just be the ugliest code I've ever written:

    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;
    	}

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Testing values in an array.

    Yes, uncommented code can be very ugly.

  8. #8
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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

Similar Threads

  1. Product of array values
    By tarkal in forum Collections and Generics
    Replies: 13
    Last Post: September 27th, 2011, 05:27 PM
  2. Problem with array values
    By Harry Blargle in forum Collections and Generics
    Replies: 5
    Last Post: September 17th, 2011, 04:05 PM
  3. need help inputting values into an array
    By pds8475 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 22nd, 2011, 09:47 PM
  4. [SOLVED] theory behind testing each element of an array.
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: February 5th, 2010, 09:04 AM
  5. Substitution of Values in Array
    By nyeung in forum Collections and Generics
    Replies: 2
    Last Post: October 26th, 2009, 08:02 PM