• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ensureCapacity() method in ArrayList

 
Komal Amaresh
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Komal Amaresh
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you sir, i understood it.

thank you very nuch Neal

with regards,
komal
 
salvin francis
Bartender
Posts: 1308
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic