• Post Reply Bookmark Topic Watch Topic
  • New Topic

Avoiding "ArrayIndexOutOfBounds" Exception when checking surrounding cells  RSS feed

 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
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?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArrayIndexOutOfBounds exception comes when you are trying to access the index which is not present on the array.

Say you have array of size 3 and if you try to access index 3 or more then it will throw exception..
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:Say you have array of size 3 and if you try to access index 3 or more then it will throw exception..

And at the other end, index -1 or less.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imagine you have Tushar's 3‑element array and you pass it to line 12 you you want to get its 3rd element. Go through line 12 carefully.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

So should I make it (i + 1) < simGrid.length?
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way of doing this, which is less space efficient but makes the code easier to read:

Create your array such that you have a "border" of cells around it.
You then iterate over this array from 1 to length -2.

All +1, -1 references to indexes will reach a valid array cell, so you don't need to do the range checking.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!