• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Vectors

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

One of the standard ways of creating a vector is

Vector v = new Vector();

This way, vector v has an internal array of size 10.But what is the incremental size of this Vector.

That is if I declare a Vector this way and later on during run time I have to add 15 elements to this vector, will this vector add those 15 elements or it will give me some exception?

What is the difference between declaring vector with :

Vector v = new Vector()

and

Vector v = new Vector(10)

Regards,
 
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This way, vector v has an internal array of size 10.But what is the incremental size of this Vector.


In vector capacity increment refers to the size by which the Vector increases it's storage capacity everytime it's filled up.

If you create a Vector in this fashion Vector v = new Vector(), the default size is 10 and capacity incerement is zero, ie it will grow as needed only, ie when elements are added.


if I declare a Vector this way and later on during run time I have to add 15 elements to this vector, will this vector add those 15 elements or it will give me some exception?



Never ever will a Vector throw an exception complaining lack of space to store data. Thus the Vector will add 15 elements each and every time allocating space for each element that's being added. It does not reserve space as it would otherwise if you specify a capacity increment (using the Vector(int initialCapacity, int capacityIncrement) contructor).


What is the difference between declaring vector with :

Vector v = new Vector()

and

Vector v = new Vector(10)



Nothing. Both creates a Vector with initial capacity of ten and capacity increment zero (ie space is reserved on an 'as needed' basis)

Cheers,
Ram.
 
Nilesh Srivastava
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ram,

But I think that if we declare Vector as:

Vector v = new Vector(20);

then the default increment value is 20. ie if Vector has to increase its capacity it will increase it by 20. ie doubling the capacity vector.

I am not sure if this occurs with

Vector v = new Vector();

Here the capacityincrement is zero, FINE. so it must be incrementing by 1 at a time.

Please help me clearing my doubts.

Regards,
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


But I think that if we declare Vector as:
Vector v = new Vector(20);
then the default increment value is 20. ie if Vector has to increase its capacity it will increase it by 20. ie doubling the capacity vector.



No.
When you use the Vector(int) constructor as you have done in your post, the the int parameter passed is the initial capacity and not the capacity increment.
Capacity increment for Vectors initialized using the no-args constructor Vector() and the constructor that accepts an int parameter (ie Vector(int)) is zero.

You can read it here in javadocs

I have posted the relevant portion below


from javadocs
----------------
public Vector(int initialCapacity)

Constructs an empty vector with the specified initial capacity and with its capacity increment equal to zero.



The only way to provide the capacity increment is using the Vector(int, int) constructor.

See the relevant section from javadoc below


from javadocs
--------------
public Vector(int initialCapacity,int capacityIncrement)

Constructs an empty vector with the specified initial capacity and capacity increment.

 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


But I think that if we declare Vector as:
Vector v = new Vector(20);
then the default increment value is 20. ie if Vector has to increase its capacity it will increase it by 20. ie doubling the capacity vector.



No.
When you use the Vector(int) constructor as you have done in your post, the the int parameter passed is the initial capacity and not the capacity increment.
Capacity increment for Vectors initialized using the no-args constructor Vector() and the constructor that accepts an int parameter (ie Vector(int)) is zero.

You can read it here in javadocs

I have posted the relevant portion below


from javadocs
----------------
public Vector(int initialCapacity)

Constructs an empty vector with the specified initial capacity and with its capacity increment equal to zero.



The only way to provide the capacity increment is using the Vector(int, int) constructor.

See the relevant section from javadoc below


from javadocs
--------------
public Vector(int initialCapacity,int capacityIncrement)

Constructs an empty vector with the specified initial capacity and capacity increment.



Cheers,
ram.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take advantage of the fact that you have access to the Java source code. A Vector with a capacityIncrement of zero or less will double its size every time it needs to grow:
reply
    Bookmark Topic Watch Topic
  • New Topic