Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Negative index for ArrayList element leading to ArrayIndexOutOfBoundsException?  RSS feed

 
Shane Jensen
Ranch Hand
Posts: 113
7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

It was my understanding that trying to access a non-existing ArrayList element leads to an IndexOutOfBoundsException, while trying to access a non-existing Array element leads to an ArrayIndexOutOfBoundsException.

When I try to run the following code, an IndexOutOfBoundsException indeed is thrown.


However, when I use a negative index, an ArrayIndexOutOfBoundsException is thrown.

What is the logic behind this? It would make a lot more sense to me if trying to access any non-existing element of an ArrayList would throw the same exception.

Thank you in advance!

Regards,
Shane
 
Charles O'Leary
Ranch Hand
Posts: 184
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that question.  I noticed that too.  Now, I finally looked into it.   

Long story short: It's kinda like the difference between NumberFormatException and IllegalArgumentException: Namely, the child class is a larger object (is more specific) than its parent
2017-06-18_20-48-07.png
[Thumbnail for 2017-06-18_20-48-07.png]
 
Shane Jensen
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Charles,

Thanks again!
 
Roel De Nijs
Sheriff
Posts: 11294
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shane Jensen wrote:What is the logic behind this? It would make a lot more sense to me if trying to access any non-existing element of an ArrayList would throw the same exception.

First of all, I would like to emphasize that this is nothing more but an implementation detail. The get() method of the List interface defines that an IndexOutOfBoundsException must be thrown if the index is out of range (index < 0 || index >= size()). So any implementation of thist List interface must adhere to this contract! But any implementation which doesn't violate the contract is fine and will do. So if you would your own ShaneList class (which implements List), you could choose to throw a NegativeIndexException if index < 0 and an IndexTooBigException if index >= size(). If both custom exception classes extend from IndexOutOfBoundsException, your class adheres to the contract and its a valid implementation (which can be used polimorphically anywhere where a List is used).

That was the intro. Now let's look at this particular method. If you check the source code of the get() method of OpenJDK, you'll notice that the implementation of this method looks like this (but once again: this is just one implementation, if you check another JDK it might be different):And the rangeCheck(index) method looks likeSo this method only checks if the index isn't too big, it doesn't check if the index is too low (less than zero). Why the latter check isn't implemented as well in this method, I have no clue about. So the IndexOutOfBoundsException is (in this case) an example of a programmatically thrown exception; the ArrayIndexOutOfBoundsException is an example of an exception thrown by the JVM. And you'll notice the difference in the stack trace too. Here's the one with a too big indexAnd here's the negative index one

Hope it helps!
Kind regards,
Roel
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
Thoughts on deprecation in Java
https://coderanch.com/t/683016/java/Deprecation-Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!