Results 1 to 4 of 4
  1. #1
    dwill19686 is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default exception in thread main java.lang.arrayindexoutofboundsexception

    Hi,

    I am coding this program out of a book on creating a Two Dimensional array and keep running into this error (exception in thread main java.lang.arrayindexoutofboundsexception).

    Could someone help me on how to correct this? Here is my code.

    public class TwoDimensionArray {


    public static void main(String[] args) {
    float myarray[][] = {
    {-56.7f},
    {500.1f, 70.70f},
    {100.9f, 0.5f, 20.20f}
    };

    System.out.println("myarray.length =" + myarray.length);

    System.out.println(myarray[0][0]);
    System.out.println(myarray[0][1]);
    System.out.println(myarray[1][0]);
    System.out.println(myarray[1][1]);
    System.out.println(myarray[2][0]);
    System.out.println(myarray[2][1]);

    }

    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Look closely at your array assignment...what would you expect each value to be? (In particular, myarray[0][1])

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

    Default

    The first row of your two dimensional array has just one element (stored at position (0,0)) so an attempt to read an element at position (0,1) causes an AIOOBE.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    dwill19686 is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    Ok. I know you both have been told this before, but i'm new and already you guys are awesome in my book. The book that I have is incorrect in the position it is setting and is what is causing the error. I'm a member for life. Thanks for your help and quick response.

Similar Threads

  1. Replies: 8
    Last Post: 03-05-2011, 03:53 AM
  2. Replies: 4
    Last Post: 02-02-2011, 04:32 AM
  3. Replies: 4
    Last Post: 10-13-2010, 09:19 AM
  4. Replies: 5
    Last Post: 04-05-2010, 02:28 PM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 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
  •