| Author |
ArrayIndexOutOfBound
|
khurram babar
Greenhorn
Joined: May 13, 2003
Posts: 4
|
|
|
any body tell me when this occurs plz tell me with brief description
|
 |
Gregg Bolinger
Ranch Hand
Joined: Jul 11, 2001
Posts: 15230
|
|
I believe this occurs when you try and access an array element that does not exist. For example, say you had: String[] i = {"Gregg", "Henry", "Jo"}; and then you said: String name = i[3]; You would probably get that error because element 3 is "out of the array bounds".
|
 |
Jessica Sant
Sheriff
Joined: Oct 17, 2001
Posts: 4313
|
|
an ArrayIndexOutOfBoundsException happens when .... ummmm.. you're accessing an array and you go out of bounds (helpful huh? ) But no, seriously -- that's exactly what happens. Lets say you have a snazzy li'l int array that holds 5 ints like this: int intArray[] = {0, 1, 2, 3, 4}; (now remember -- arrays start at index 0) So... if I try to access spaces 0 thru 4, everything's fine -- no problems -- there are numbers in all those spots, and all those spots actually EXIST. System.out.println( intArray[0] ); System.out.println( intArray[1] ); System.out.println( intArray[2] ); System.out.println( intArray[3] ); System.out.println( intArray[4] ); But what happens if I try to access BEYOND that last spot? System.out.println( intArray[5] ); *boom* ArrayIndexOutOfBoundsException get it? So... what do you think will happen if you try to access a negative index? like this: System.out.println( intArray[-1] );
|
- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
|
 |
jim gotti
Ranch Hand
Joined: Jul 02, 2002
Posts: 36
|
|
the ole +1 -1 error. remember array indecies(spelling?) run from 0 to n-1, where n is the length of the array at creation. So as the above posters pointed out.... an array of length say 5, has indecies from 0 to 4 (n-1)
|
 |
 |
|
|
subject: ArrayIndexOutOfBound
|
|
|