Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When IndexOutOfBoundsException is thrown

 
Paulo Gervásio
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm new in this forum.
I will do the SCJP exam, but I still have many doubts...
One of then is about when the RuntimeException IndexOutOfBoundsException is thrown

I could see that this exception is a super class from ArrayIndexOutOfBoundsException and StringIndexOutOfBoundsException.

But is it used in other case?

Thanks.

 
Tom Reilly
Rancher
Posts: 618
 
Paulo Gervásio
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tom Reilly, How are you?

After your post I found the answer!
Look:

The IndexOutOfBoundsException is thrown when you try access a null index in a List object:



Yeah! The method get throws IndexOutOfBoundsException...
But... Why it don't throws null, like the Map Collection, for example?


Anyone?
Thanks.


 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm...I would have thought an ArrayList would throw an ArrayIndexOutOfBoundsException but it appears that arrays (lower case a) throws that exception. I don't understand what you mean by:
But... Why it don't throws null, like the Map Collection, for example?


Are you asking why it doesn't return null?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paulo wrote:when you try access a null index in a List object

I would say "when you try to access a list of objects with an index out of the range of the list".

Paulo wrote:Why it don't throws null, like the Map Collection, for example?

Usual Maps are not bounded, you don't use index to access them, but keys. A key is not ouf of range. It exists, or it doesn't. Some custom maps, like org.apache.commons.collections.map.LinkedMap, use indexes to access the values. IndexOutOfBoundsException will also be thrown if the index used is out of range.
 
Paulo Gervásio
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay.
I meant that the Objects of Map Collection returns null when a invalid index is called. Example:



The code above returns null and not throws a exception... understood?

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not an index, this is a key. In a Map, you associate some values to some keys, and retrieve the values via the keys. In your example, you are using Integers for keys. These are not indexes, these are still keys.
1 -> "One"
2 -> "Two"
map.get(2) : "Two"
map.get(3) : null
IndexOutOfBoundsException will never be thrown, because this is not an index you are using. The value associated to 3 does not exist, so null is returned.

List are different. You can access elements via an index. This index ranges from 0 to the size of the list(excluded). If the index is out of this range, IndexOutOfBoundsException will be thrown.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EDIT: I posted this at the same time as Christophe Verré so this is probably redundant.

Lists are all about indexes. Maps are all about key-value pairs.

As Christophe Verré suggests, I think you are confusing indexes with keys. In Lists, you can have multiple null values. You also always know when an index is valid (0 to List.length - 1) so trying to access anything outside those indexes is considered a programming error. For maps, you don't always know the keys so asking for the corresponding value of a key is not considered a programming error and therefore returns null if the key does not exists in the map.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic