programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# checking for neighbours in array

Greenhorn
Posts: 15
Hi -
I've got a 2-dimensional array with 10 rows and 10 columns.
I need to be able to check what the value of each of the array element's neighbours are - that is the eight elements that are one column and or row removed - and if they are a specific value, a counter gets incremented.
Now, I can loop through the array
for(int row = 0; row < array.length; row++)
{
for( int column = 0; column < array[row].length; column++)
checking for the neighbours of each element by checking array[row-1][column-1] or array[row][column+1] or array[row+1][column+1]... etc
But of course, if the element I'm checking has a row value of 0, then I get a "array out of bounds" error for trying to chec array[row-1][column]
and so on for all the array elements that are on the "edge" of the array, so to speak.
I've tried coping with various if and else statements, as in "if row is equal to zero or column is equal to zero, check only these index values" but I end up with an immense and ugly if - else list that does not seem to work anyway.
What would be a more efficient way of doing this?

Greenhorn
Posts: 6
Firstly,
when looping through arrays the INNER (or second loop) MUST have its termination value set to arrayName.length-1. That should eliminate the ArrayOutOfBoundsException at runtime.
(Basically your trying to iterate thorough the columns and elements in an array that exceed the length or actually dont exist.)
To be honest im not quite sure what you mean by checking for neighbours. If you reply with a better explanation i could try and help..
Hope the above helps.

lowercase baba
Bartender
Posts: 12589
50
Alex,
That won't completely eliminate the exceptions found. we still have the problem of testing the neighbors of [0,0] and trying for [-1,0] - we're out of bounds.
you might be able to write two methods, something like

then, inside your main loops, you'd do:

in other words, for any given element, you only test the row ABOVE it if you are in row 1 or greater. you only test the row BELOW it if you are at 9 or less (otherwise those rows don't exist, and you'd get your outof bounds exception). you ALWAYS test the row your element is IN.
so now that we know which rows to test, what do we do with them? well, there are two kinds of rows - the one our 'center' element is in that we DON'T want to test, and rows where we want to test all three elements. that's why i made the two methods.
writing the "sameRow" method, we only test to the left if y > 0, and only to the right if y < 9.
now we write the "differentRow" method. AHA!!! we need to test the elements to the left and right - we've already got code for that!!! so i call that method, then test the center one.
i'm not sure how your counting the total number of hits. these methods may return ints for a sum of the total number of hits, or you may just want them to return booleans if you only care if there is "one or more" hits in the 8 neighbors.
so, have i TOTALLY confused you now?
;-)

Masha Stekker
Greenhorn
Posts: 15
OK - sorry for being unclear I'll have another go.
My problem is not because of the length -1 issue . That was a typo I am ashamed to say, and my actual code does have it limited to array. lenght -1.
Here goes my second try at explaining:
I have a 2d array.
I need to check what the values of the "neighbours" of each element in the array is:
For example, array[1][1] will have neighbours array[1][2], array[2][2], array[2][1], array[2][0], array[1][0] , array[0][0], array[0][1], array[1][2]
Which can be expressed as array[row][count] has neigbours array[row][column+1], array[row+1][column+1], array[row+1][column] etc.
Each array element has eight neighbours, if it is not on the "edge of the grid", as in, if it does not have a row or column value of 0 or a row or column value of array.length-1
Now, if a element has the same value as 3 of its neighbours a counter variable needs to be incremented.
To try to find the values of each elements neighbours, I loop through the rows and columns of the array, and compare each element to the array[row][column+1] etc as explained above to see if it has the same value as its neighbour.
But I get the out of bounds error if the element I'm checking has a row or column value of 0 or array.lenght-1 because if row is 0, then array[row-1][column] gives me a -1 value for row. And so on for column at 0 etc.
I can avoid this by if - else statements, that is, if(row == 0) then only check certain neighbours, not those that decrement row. But this gives me a very long ugly list of if-elses that confuse me and somewhere in there lurks a logical mistake.
I was wondering if there is a better way of checking for the "neighbours" of a 2d array.

Masha Stekker
Greenhorn
Posts: 15
OK Fred - I see you answered while I was still typing. I'll mull over your answer a bit...

 Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS