Results 1 to 7 of 7
  1. #1
    3xpr1ment is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default How to handle ArrayIndexOutOfBoundsException

    Hi all,

    I am trying to find a way to handle an ArrayIndexOutOfBoundsException.


    Java Code:
    int [] plegma = new int[10];
    int x = plegma[11]);
    this will give an exception. Is there a way (maybe try-catch) to set the variable to the previous code to 0 i.e every time an index is out of bound to set it to 0?

    Thank you for your help.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by 3xpr1ment View Post
    Hi all,

    I am trying to find a way to handle an ArrayIndexOutOfBoundsException.


    Java Code:
    int [] plegma = new int[10];
    int x = plegma[11]);
    this will give an exception. Is there a way (maybe try-catch) to set the variable to the previous code to 0 i.e every time an index is out of bound to set it to 0?

    Thank you for your help.
    No, don't catch it. The secret here is to write code that is smart enough not to run into this problem. If you loop through an array for instance, use the array's length as the stopping point.

  3. #3
    3xpr1ment is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    Thanks for your help Fubarable!

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Fubarable View Post
    If you loop through an array for instance, use the array's length as the stopping point.

    And ... the stopping point must be 1 smaller than the array's length. so in a for loop check for < and not <=

    for (int i=0; i < plegma.length; i++ {
    yourcode;
    }

    at the beginning, this concept is a little confusing and needs getting used to. have fun.

  5. #5
    3xpr1ment is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default

    Thank you j2me64. Actually I got a 2d matrix where the state of each element depends on the state of its neighbors . The elements on the borders have no neighbors from the one side.
    I wanted to know what is the best practice (acquire good programming habits :)) in this case, to handle the exception or add an additional row, column on this side it and set the its state to 0.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I wouldn't use exception here or an extra row or column but rather use simple math to know when I'm at the boundaries and to not go beyond. Math.min and Math.max can be helpful. For instance look at post #16 here: MineSweeper!

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,514
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by 3xpr1ment View Post
    Thank you j2me64. Actually I got a 2d matrix where the state of each element depends on the state of its neighbors . The elements on the borders have no neighbors from the one side.
    I wanted to know what is the best practice (acquire good programming habits :)) in this case, to handle the exception or add an additional row, column on this side it and set the its state to 0.
    Try both alternatives: one version where you check whether or not your in a corner or on an edge and another where you've added two rows and two columns around the actual values of the matrix; note that you have to adjust your loop bounds to:

    Java Code:
    for (int i= 1; i < array.length-1; i++) ...
    ... and see which version you like most (I like the additional rows/columns trick most because there is no bounds checking needed).

    kind regards,

    Jos

Similar Threads

  1. ArrayIndexOutOfBoundsException
    By Corey in forum New To Java
    Replies: 5
    Last Post: 02-02-2010, 01:25 AM
  2. ArrayIndexOutOfBoundsException
    By flaskvacuum in forum New To Java
    Replies: 6
    Last Post: 07-14-2009, 05:36 PM
  3. [SOLVED] ArrayIndexOutOfBoundsException
    By thelinuxguy in forum Advanced Java
    Replies: 1
    Last Post: 05-07-2009, 10:34 PM
  4. ArrayIndexOutofBoundsException help
    By filly444 in forum New To Java
    Replies: 9
    Last Post: 09-03-2008, 05:16 PM
  5. ArrayIndexOutOfBoundsException
    By daredavil82 in forum New To Java
    Replies: 2
    Last Post: 12-14-2007, 09:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •