This week's book giveaway is in the JavaScript forum.
We're giving away four copies of JavaScript Promises Essentials and have Rami Sarieddine on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes When IndexOutOfBoundsException is thrown Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "When IndexOutOfBoundsException is thrown" Watch "When IndexOutOfBoundsException is thrown" New topic
Author

When IndexOutOfBoundsException is thrown

Paulo Gervásio
Greenhorn

Joined: Jun 12, 2010
Posts: 4
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

Joined: Jun 01, 2010
Posts: 618
I googled IndexOutOfBoundsException and the first returned item was Uses of Class java.lang.IndexOutOfBoundsException:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/class-use/IndexOutOfBoundsException.html

Paulo Gervásio
Greenhorn

Joined: Jun 12, 2010
Posts: 4
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

Joined: Jun 01, 2010
Posts: 618
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.

[My Blog]
All roads lead to JavaRanch
Paulo Gervásio
Greenhorn

Joined: Jun 12, 2010
Posts: 4
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Jun 01, 2010
Posts: 618
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When IndexOutOfBoundsException is thrown