Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

ensureCapacity() method in ArrayList

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you sir, i understood it.

thank you very nuch Neal

with regards,
komal
 
Bartender
Posts: 2900
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic