• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why i'm getting ArrayIndexOutOfBoundsException in arraylist?  RSS feed

 
Gautham Muralidharan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have commented out lines 12 & 13.
When I add line 12 to the program, it throws arrayindexoutofboundsexception.
But when I remove line 12 & add line 13 to the program, it throws indexoutofboundsexception.
Why does line12 throw arrayindexoutofboundsexception while i'm dealing with ArrayList?

output when i add line12 in the program.

output when i add line13 instead of line12 in the program.
 
Gautham Muralidharan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correction: i've typed line 12 and 13 for line 13 and 14.

Please consider line 13 where i've mentioned line 12 and line 14 where i've mentioned line 13.

Sorry Friends.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi gautham,in java array index starts from 0,so if you code like this-a.get(-1),you are requesting arraylist to give you the element at index -1 which is out of bound of arraylist index or illegal index(means index is either negative or greater than size of array) in java,so you get the exception ArrayIndexOutOfBoundsException,but if you will request the arraylist to give you the element at out of range index then you will get IndexoutOfBoundsException.
Actually arraylist uses an array backing for storing the elements and you can define its initial capicity during initialization and it will grow its size by itself when elements exceeds the initial capicity resulting in growing the size.
See these link for further understanding-oracle api--->IndexOutOfBoundsException , ArrayIndexOutOfBoundsException , ArrayList.

Hope this will Help!

Kind regards,
Praveen.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one more thing here in arraylist,out of range means it is possible to get that index but before that you need to fill such elements in list.before filling that index it is empty,so you can say out of range as index with empty element,even though you can fill that empty element by adding null object and afterwards get it by getter.try it..
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:Hi gautham,in java array index starts from 0,so if you code like this-a.get(-1),you are requesting arraylist to give you the element at index -1 which is out of bound of arraylist index or illegal index(means index is either negative or greater than size of array) in java,so you get the exception ArrayIndexOutOfBoundsException,but if you will request the arraylist to give you the element at out of range index then you will get IndexoutOfBoundsException.

If you use a language like Python, this may seem confusing, because in Python element -1 means the last one. Just to repeat what Praveen said, in Java, -1 really means -1, which is not a valid index for any array or List.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look in the API documentation, it tells you all the details. That link was to the specification in the List interface, but you find exactly the same happens in the implementations, for example:- ArrayList and LinkedList. Those three links all say that method throws an Exception if index is less than 0.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!