• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList and Vector size incrementation

 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read on one site http://www.java-questions.com/collections_interview_questions.html
that, A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.
But what I read on Javadoc, that said, we have capacity and capacityIncrement variables, that actually decide by what value, the arraylist or vectors array size should be increased. That means we can control the array size when it falls out of size. Then what does the statement listed in above site mean?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key word you are missing is 'defaults'. " Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent." So in the absence of a capacityIncrement, Vector doubles its size. In the JavaDocs it tells us that if the capacityIncrement is <=0 then the behavior is to double in size. We also see that the constructors that don't have capacityIncrement parameters will set the increment to 0 (meaning double).

I disagree with the statement about the default ArrayList size increment - according to the Docs, the behavior is not defined, so it may increase by 50 percent under some conditions, or in some versions of Java, but may increment in some other way on other versions/conditions.
 
Henry Wong
author
Marshal
Pie
Posts: 21226
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I think the key words to note here are "defaults to".... meaning what happens when you don't set any of those increment attributes.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21226
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

WOW!!! ... I got beaten to the answer by one whole second !!!

Henry
 
MaheshS Kumbhar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve and Henry
 
Mohana Rao Sv
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know why the developers chosen different approach for incrementing the size for ArrayList and Vector?
 
Henry Wong
author
Marshal
Pie
Posts: 21226
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohana Rao Sv wrote:I would like to know why the developers chosen different approach for incrementing the size for ArrayList and Vector?


Well, asking why and specifically related to the mind of the java designers requires speculation -- we can only guess. However, since the Vector class was part of Java 1.0, and the ArrayList class was added much later, with the collection framework of Java 1.2, I would speculate that it was done by two different people.

Henry
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's also possible that experience showed that Vector's approach wasn't the most efficient in "average" cases, and ArrayList improved on it. But that's pure speculation. But there was enough time for people to study the usage if they wanted to.
 
Mohana Rao Sv
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Matthew, @Henry Thanks your comments.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic