aspose file tools*
The moose likes Java in General and the fly likes Vectors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Vectors" Watch "Vectors" New topic
Author

Vectors

Nilesh Srivastava
Ranch Hand

Joined: Aug 29, 2003
Posts: 70
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,
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489


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

Joined: Aug 29, 2003
Posts: 70
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

Joined: Jan 24, 2005
Posts: 489


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

Joined: Jan 24, 2005
Posts: 489


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.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vectors