aspose file tools*
The moose likes Beginning Java and the fly likes ensureCapacity() method in ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ensureCapacity() method in ArrayList" Watch "ensureCapacity() method in ArrayList" New topic
Author

ensureCapacity() method in ArrayList

Komal Amaresh
Ranch Hand

Joined: Oct 06, 2008
Posts: 67

Sir,

I' m unable to understand the purpose of the ensureCapacity() method in ArrayList. It does not help in any way.The following is my code.


I had commented the last 2 objects passed in the arrayList which reduces its its size to 4. Later I removed the comments which increases its size to 6. The ensureCpacity as is set to 5 does not give an exception or an error when elements are removed or added. please tell me the use of the ensureCapacity() method.

Thanking you,

with regards,
komal
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
Here are the javadocs.


The docs say that the method ensures that you can add another 5 elements atleast. So the call actually increases the capacity by 5 and not restrict it.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
An ArrayList is a wrapper around an array. Arrays cannot be resized, so if you want to increase the size of the array, you need to create a new array of the required size and then copy all the elements from the old array to the new array.

So, in simple terms, assume you have an ArrayList with 5 elements in it. If you call the add method to add another element, what will actually happen in the background is that a new array of size 6 will be created, all the existing elements from the old array will be copied into the new array and then the new element will be added to the new array. Now, what if you were going to to add a thousand new elements to the ArrayList by calling get() a thousand times ? Each time, a new array would be created and all the elements copied from the old array. This is inefficient.

However, if you know you are going to add a thousand elements to the ArrayList, if you call the ensureCapacity method first, this will create a new array with room for all those new elements. Now, every time you call get, the array will already be large enough to accomodate the new element, so no more array creation and copying will be needed.


Joanne
Komal Amaresh
Ranch Hand

Joined: Oct 06, 2008
Posts: 67
Sir,

why do we have to initialize the capacity of an ArrayList or reinitialise it later when collections are primarily used for dynamic assignment.

Thanking you,

with regards,
komal
Komal Amaresh
Ranch Hand

Joined: Oct 06, 2008
Posts: 67
Thank you sir, i understood it.

thank you very nuch Neal

with regards,
komal
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Komal Amaresh wrote:Sir,

why do we have to initialize the capacity of an ArrayList or reinitialise it later when collections are primarily used for dynamic assignment.

Thanking you,

with regards,
komal


(dont call me sir Please)


1. We do not have to initialise the ArrayList capacity at all its not mandatory.
2. From an optimizations Point of view:

lets suppose n=2 (array size increases by 2)

array : [][]
adding 1,2
array : [1][2]
adding 3
array : [1][2][3][] NEW ARRAY is Created
adding 4
array : [1][2][3][4]
adding 5
array : [1][2][3][4][5][] NEW ARRAY is Created
adding 6
array : [1][2][3][4][5][6]



lets suppose n=5 (array size increases by 5)

array : [][][][][]
adding 1,2
array : [1][2][][][]
adding 3
array : [1][2][3][][]
adding 4
array : [1][2][3][4][]
adding 5
array : [1][2][3][4][5]
adding 6
array : [1][2][3][4][5][][][][][] NEW ARRAY is created



what you would prefer in this case: n=2 or n=5 ?

My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ensureCapacity() method in ArrayList