ArrayIndexOutOfBoundsException

Discussion in 'Java' started by tiewknvc9, Feb 15, 2006.

  1. tiewknvc9

    tiewknvc9 Guest

    Hi.

    Im getting an ArrayIndexOutOfBoundsException when using an array, and
    while I know I can use a try catch statement to get around this
    problem, I was wondering if there was a way to test to see if the
    element exists instead.

    for instance.

    if (myArray[67].exists){
    //then do something with myArray[67]
    }

    thanks.
    tiewknvc9, Feb 15, 2006
    #1
    1. Advertising

  2. tiewknvc9

    tiewknvc9 Guest

    I got it

    if (67 > myArray.length){
    //not exception!
    }
    tiewknvc9, Feb 15, 2006
    #2
    1. Advertising

  3. tiewknvc9

    Alan Krueger Guest

    tiewknvc9 wrote:
    > I got it
    >
    > if (67 > myArray.length){
    > //not exception!
    > }


    Actually, I think you mean "index < myArray.length". If the index is
    greater than the length (or even equal to it), you will get an exception.
    Alan Krueger, Feb 16, 2006
    #3
  4. tiewknvc9

    Finomosec Guest

    Alan Krueger schrieb:
    > tiewknvc9 wrote:
    >> I got it
    >>
    >> if (67 > myArray.length){
    >> //not exception!
    >> }

    >
    > Actually, I think you mean "index < myArray.length". If the index is
    > greater than the length (or even equal to it), you will get an exception.


    Be aware, that arrays in Java are zero-based.

    A typical loop over an array looks like this:

    String[] strings = new String[]{"string0", "string1", "string2"};

    // zero-based:
    // strings[0] ==> "string0"
    // strings[1] ==> "string1"
    // strings[2] ==> "string2"
    // strings.length == 3

    for (int i = 0; i < strings.length; i++) {
    String current = strings;
    // do something with it ...
    System.out.println(i + ": " + current);
    }

    This way you will never get any ArrayIndexOutOfBoundsExceptions ...

    Greetings Finomosec;
    Finomosec, Feb 16, 2006
    #4
  5. tiewknvc9

    Roedy Green Guest

    On Thu, 16 Feb 2006 21:53:25 +0100, Finomosec <>
    wrote, quoted or indirectly quoted someone who said :

    >Be aware, that arrays in Java are zero-based.


    in other words if you have an array of 10 elements ,they are numbered
    0 to 9.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Feb 17, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sudhir
    Replies:
    1
    Views:
    785
    John C. Bollinger
    Aug 27, 2003
  2. Johan
    Replies:
    5
    Views:
    531
    Paul Lutus
    Oct 15, 2003
  3. siarsky
    Replies:
    0
    Views:
    574
    siarsky
    Dec 23, 2003
  4. Aloys Oberthuer
    Replies:
    3
    Views:
    7,496
    Aloys Oberthuer
    Apr 22, 2004
  5. Simon Bieri
    Replies:
    2
    Views:
    4,689
    Simon Bieri
    Jun 15, 2004
Loading...

Share This Page