| Author |
What is going on? new ArrayList( 10 ) throws Exception when add( 2, "something" )
|
Dan Bizman
Ranch Hand
Joined: Feb 25, 2003
Posts: 387
|
|
I created the below ArrayList (java 1.4.2_03) and it's throwing an out of bounds exception! What could I be doing wrong? Is there a bug in 1.4.2_03?
|
 |
Nathaniel Stoddard
Ranch Hand
Joined: May 29, 2003
Posts: 1258
|
|
|
Capacity and size are two different things. When you supply and index for a set(int, Object), the int has to be within the range of 0 to the size. Despite setting the capacity to 34, the size is still 0, and therefore you get an exception thrown.
|
Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
|
To be more specific, the size is the number of elements currently in the list, the capacity is the number of elements the list can hold before the internally used array needs to be resized. Setting the capacity is a pure performance optimization.
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
 |
|
|
subject: What is going on? new ArrayList( 10 ) throws Exception when add( 2, "something" )
|
|
|