• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between ArrayList(int initial capacity) and ensureCapacity().  RSS feed

 
Ritesh raushan
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference between
1-ArrayList(int initial capacity);//constructor

2-ensureCapacity();//predefined method

my question is from capacity i think if we given capacity in a ArrayList constructor or in ensureCapacity() both will do same for resizing the
capacity of internal data structure...
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from initialCapacity JavaDoc :

@param initialCapacity the initial capacity of the list


from ensureCapacity JavaDoc :

Increases the capacity of this ArrayList instance, if
necessary, to ensure that it can hold at least the number of elements
specified by the minimum capacity argument

@param minCapacity the desired minimum capacity


initialCapacity is passed only once at the time of object creation.

the important difference is that if you pass 20 as initialCapacity , then exactly 20 buckets will be created.
but in case of ensureCapacity some time buckets counts(capacity) *can be more than what you pass as an argument*.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!