aspose file tools
The moose likes Java in General and the fly likes What is going on? new ArrayList( 10 ) throws Exception when add( 2, Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "What is going on? new ArrayList( 10 ) throws Exception when add( 2, "something" )" Watch "What is going on? new ArrayList( 10 ) throws Exception when add( 2, "something" )" New topic
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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: What is going on? new ArrayList( 10 ) throws Exception when add( 2, "something" )
 
Similar Threads
What does this code mean?
IndexOutOfBound Exception in ArraList.isEmpty()
Properties not properly set in tomcat mail session.
Strange CLASSPATH Problem...
Null Pointer Exception