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 will have neighbours array, array, array, array, array , array, array, array
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.