• Post Reply Bookmark Topic Watch Topic
  • New Topic

Vector capacity in Java  RSS feed

 
chamini prashakthi
Ranch Hand
Posts: 127
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the vector class that I have created,
Vector v= new Vector(3,2);
        System.out.println("v.capacity: " +v.capacity());
        v.addElement(new Integer(1));
        v.addElement(new Integer(2));
        v.addElement(new Integer(3));
        v.addElement(new Integer(4));
        System.out.println("v.size "+v.size());
        System.out.println("v.capacity: " +v.capacity());

this shows result as :

v.capacity: 3
v.size 4
v.capacity: 5


if I change
Vector v= new Vector(7,2);
it gives
v.capacity: 7
v.size 4
v.capacity: 7

So I want to know what is the actual output of this second capacity???Why the second out put didn't give me 9 as capacity size???
 
Stephan van Hulst
Saloon Keeper
Posts: 7929
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Capacity increases when the vector overflows. When your vector's size is 7 and you add 4 elements, it doesn't overflow.
 
Campbell Ritchie
Marshal
Posts: 56221
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using Vector, which is usually regarded as legacy code? Why are you investigating its capacity? As long as you don't run out of memory, capacity is an implementation detail you don't need to know about. Various old features of Vector, including the ability to set a predetermined capacity increment, are nowadays regarded as not ideal, and I would think a capacity method doesn't count as best practice either.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!