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.
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?
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.
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.
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.
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.