Win a copy of Securing DevOps this week in the Security forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Vector capacity in Java  RSS feed

 
Ranch Hand
Posts: 139
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???
 
Saloon Keeper
Posts: 8462
155
  • 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.
 
Sheriff
Posts: 57858
178
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!