• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Avoiding "ArrayIndexOutOfBounds" Exception when checking surrounding cells

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79406
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79406
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic