This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes ArrayList and Vector size incrementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ArrayList and Vector size incrementation" Watch "ArrayList and Vector size incrementation" New topic
Author

ArrayList and Vector size incrementation

MaheshS Kumbhar
Ranch Hand

Joined: Sep 24, 2009
Posts: 188
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?

I am slow but sure
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40


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

Henry
MaheshS Kumbhar
Ranch Hand

Joined: Sep 24, 2009
Posts: 188
Thanks Steve and Henry
Mohana Rao Sv
Ranch Hand

Joined: Aug 01, 2007
Posts: 485

I would like to know why the developers chosen different approach for incrementing the size for ArrayList and Vector?


ocjp 6 — Feeding a person with food is a great thing in this world. Feeding the same person by transferring the knowledge is far more better thing. The reason is the amount of satisfaction which we get through food is of only one minute or two. But the satisfaction which we can get through the knowledge is of life long.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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

Joined: Apr 06, 2010
Posts: 4375
    
    8

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

Joined: Aug 01, 2007
Posts: 485

@Matthew, @Henry Thanks your comments.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: ArrayList and Vector size incrementation