• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayIndexOutOfBound

 
khurram babar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any body tell me when this occurs plz tell me with brief description
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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] );
 
jim gotti
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic