Results 1 to 4 of 4
- 02-03-2011, 05:27 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
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]);
}
}
- 02-03-2011, 05:31 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,638
- Rep Power
- 12
Look closely at your array assignment...what would you expect each value to be? (In particular, myarray[0][1])
- 02-03-2011, 05:31 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 02-03-2011, 05:49 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By paul in forum New To JavaReplies: 8Last Post: 03-05-2011, 04:53 AM -
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at wee
By Azaz in forum New To JavaReplies: 4Last Post: 02-02-2011, 05:32 AM -
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By ladyvie in forum New To JavaReplies: 4Last Post: 10-13-2010, 10:19 AM -
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 102
By dark_metal in forum New To JavaReplies: 5Last Post: 04-05-2010, 03:28 PM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 11:55 PM
Bookmarks