| Author |
Collections out of range
|
shuba gopal
Ranch Hand
Joined: May 12, 2011
Posts: 76
|
|
The following code is an example in K & B. Why are # 5 and 6 "out of range"? # 5 is included in map but why the comment "out of range". why does #6 throw error?
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2816
|
|
They're out of range for the submap, not the original map. The submap only has keys between "b" and "g" (inclusive of "b" but not of "g") because that's how the submap was created.
#5 is OK because even though "r" is outside submap's range from "b" to "g", "r" isn't being added to the submap, but to the main map. That's fine, as the main map has no restrictions. The submap just won't ever show the "r" entry, as it's out of the submap's range.
#6 causes an error because "p" is outside the submap's range, and the code is specifically trying to add the "p" into the submap. That's not allowed, so you get an error.
|
 |
Udara Amarasinghe
Ranch Hand
Joined: Aug 17, 2009
Posts: 109
|
|
Hi shuba,
I agree with Mike. It's simple on line 4 you have made submaps range only from 'b' to 'g'(but actually 'f'). So you can only add keys range of 'b' to 'f'. But you have try to add keys like 'r' and 'p' they are not in the key range of submap. That's why you got that IllegalArgumentExceptions.
|
 |
shuba gopal
Ranch Hand
Joined: May 12, 2011
Posts: 76
|
|
|
Hi Mike and Udara, thanks
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
|
Note that this behaviour is documented in the Javadoc page of SortedMap, NavigableMap and TreeMap.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: Collections out of range
|
|
|