• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList and Vector size incrementation

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve and Henry
 
Ranch Hand
Posts: 485
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Matthew, @Henry Thanks your comments.
 
Switching from electric heat to a rocket mass heater reduces your carbon footprint as much as parking 7 cars. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic