I have a update method that looks at the surrounding cells of a given cell (Only: left, right, top, bottom) and if it finds a looked for condition it will do something.
My problem is that I get a bunch of ArrayIndexOutOfBounds Exceptions on the following code when executing it:
The Exception is thrown on line 12 for a (1, 2) array, a (2, 1) array, and a (1,4) array.
What am I doing wrong here?
Is there any better or more efficient way to do it?
Thank you for the replies, but I already know that.
I need help with my code to find just exactly WHERE the index goes out of bounds.
Because line 12 looks correct to my eyes and thats what confuses me.
If I take the size 3 array and pass it through line 12 the index is between 0 - 2 (0, 1, 2).
2 being smaller than length of array of 3 it would continue onto next condition of simGrid[i + 1][j], which then of course throws exception because it would try to acces 2 + 1 = 3 which does not exist.
First half: spot on
Second half: don't know. If you only want to iterate array elements and compare them with the next element, then yes, you must stop at the penultimate element:Remember that loop will not be executed for a 1‑element array.
And i + 1 < myArray.length is equivalent to i < myArray.length - 1