# Vector

thomas davis
Ranch Hand
Posts: 207
Please somebody explain me about the significance of the digits given in the vector's constructor in the following cases
Case 1)Vector objVector = new Vector(5,10);
Case 2)Vector objVector = new Vector(5);
Case 3)Vector objVector = new Vector();

Ta Ri Ki Sun
Ranch Hand
Posts: 442
from Vector API
Constructor Summary
Vector()
Constructs an empty vector so that its internal data array has size 10 and its standard capacity increment is zero.
Vector(Collection c)
Constructs a vector containing the elements of the specified collection, in the order they are returned by the collection's iterator.
Vector(int initialCapacity)
Constructs an empty vector with the specified initial capacity and with its capacity increment equal to zero.
Vector(int initialCapacity, int capacityIncrement)
Constructs an empty vector with the specified initial capacity and capacity increment.

thomas davis
Ranch Hand
Posts: 207
Case 1)Vector objVector = new Vector(5,10);
If I have a vector intitialized as given above,and I add 6 th element to it ,How the vector will react to it?How does the internal array get incremented?
What is the significance of 5 and 10?
Does it give a crappy performance?
What do I have to do it for better performance?

Ta Ri Ki Sun
Ranch Hand
Posts: 442
Originally posted by thomas davis:
Case 1)Vector objVector = new Vector(5,10);
If I have a vector intitialized as given above,and I add 6 th element to it ,How the vector will react to it?How does the internal array get incremented?

by adding the 6th element the Vectors size becomes 6, but the internal data array's size becomes 15, add a 16th and it becomes 25, and so on.
What is the significance of 5 and 10?

5 is
the initial capacity of the vector.
, and 10 is
the amount by which the capacity is increased when the vector overflows.

Does it give a crappy performance?

generally yes, for quite a few reasons, consider these tho, its synchronized and you may not need it to be, and you'd also have to trimToSize() when removing elements, depending on whether you've given it a rather big task or not, obviously for a default capacity of 5 and increment by 10 as given in your example this isn't needed.

What do I have to do it for better performance?

switch to a different collection, have a look at the java.util package for something that suits your requirements, you'll have to ask yourself a few questions first tho, like do you need it to be synchronized, how will you access it and when, how often are objects added/removed, does it need to be sorted, do you need a key or is the index enough.
for behaviour very similar to the Vector minus synchronization you can look at ArrayList.

Ta Ri Ki Sun
Ranch Hand
Posts: 442
also have a look at how someone considers various factors to determine the best data structure to use
Which Collection/Data Structure
[ June 17, 2003: Message edited by: Ta Ri Ki Sun ]

peter greaves
Ranch Hand
Posts: 51
the 1.4 API strongly suggests that Vector is included for backwards compatability - so existing programs won't break. if you want synchronisation,

will give it to you in an ArrayList, or you could easily wrap an ArrayList in yr own implementation and selectively implement the thread safety you want (e.g. you might decide to sync for mutation, allow "dirty" reads). arraylist supports initial capacity arg in the constructor. the entire Vector class could be considered a candidate for deprecation. its interface (retrofitted with support for List in 1.2) is poor, and performance is not great.
of course, you might be stuck with a pre-1.2 runtime. iwc, these words are pointless

regs
peter

thomas davis
Ranch Hand
Posts: 207
quote:
--------------------------------------------------------------------------------
Originally posted by thomas davis:
Case 1)Vector objVector = new Vector(5,10);
If I have a vector intitialized as given above,and I add 6 th element to it ,How the vector will react to it?How does the internal array get incremented?
--------------------------------------------------------------------------------
by adding the 6th element the Vectors size becomes 6, but the internal data array's size becomes 15, add a 16th and it becomes 25, and so on.

quote:
--------------------------------------------------------------------------------
What is the significance of 5 and 10?
--------------------------------------------------------------------------------
5 is
quote:
--------------------------------------------------------------------------------
the initial capacity of the vector.
--------------------------------------------------------------------------------
, and 10 is
quote:
--------------------------------------------------------------------------------
the amount by which the capacity is increased when the vector overflows.
--------------------------------------------------------------------------------

quote:
--------------------------------------------------------------------------------
Does it give a crappy performance?
--------------------------------------------------------------------------------

generally yes, for quite a few reasons, consider these tho, its synchronized and you may not need it to be, and you'd also have to trimToSize() when removing elements, depending on whether you've given it a rather big task or not, obviously for a default capacity of 5 and increment by 10 as given in your example this isn't needed.

QUESTION:
Vector objVector = new Vector();
by adding the one element int the vector, what would be the Vectors size?, What would be the internal data array's size ?, add a 2nd element what would be the internal array size?

Ta Ri Ki Sun
Ranch Hand
Posts: 442
QUESTION:
Vector objVector = new Vector();
by adding the one element int the vector, what would be the Vectors size?, What would be the internal data array's size ?, add a 2nd element what would be the internal array size?

I'm not sure why you're so interested in this Vector, avoid using it, but I'll help you understand it anyway, because other collections are very similar in many ways.
anyhat, the default constructor :
Vector
public Vector()Constructs an empty vector so that its internal data array has size 10 and its standard capacity increment is zero.

and
capacityIncrement
protected int capacityIncrementThe amount by which the capacity of the vector is automatically incremented when its size becomes greater than its capacity. If the capacity increment is less than or equal to zero, the capacity of the vector is doubled each time it needs to grow.

that should answer the other half, but in a nutshell, after adding the first element, in fact the second as well, the internal data array size remains at 10, while the vectors size starts at 0, and increments each time you add an element, so asking a Vector for its size will return an int representing the number of elements you've added to it.

BTW, I posted the link to the API doc so you can look up questions like that, I'm really not trying to be funny/nasty or anything but you'll find that many questions in these forums can be found by looking it up in the documentation, and I understand that sometimes you want an answer now, but also often the delay in response on the forum is alot longer than the seconds to minutes it takes to find the answer yourself, and as a bonus you learn more.
anyhat, forgive what may appear to be a lecture, its good to ask questions, but better when you've tried to look it up first