Reputation: 951
i recently started learning Java. I was studying Vectors and I came across various methods of declaring a Vector
Vector( )
Vector(int size)
Vector(int size, int incr)
Vector(Collection c)
I was able to understand the first two types but was not able to understand the increment type in 3rd type and what and when to use 4th type.
plz explain.
Upvotes: 4
Views: 562
Reputation: 12269
Vectors use an underlying array to store their elements. As you know, an array's capacity is fixed. Once you've said the array size is 20, that will never change:
int[] array = new int [20];
For the 3rd constructor, take for example:
Vector v = new Vector(20, 10);
What that means, is that even though the vector is initially empty, it has an underlying array of size 20. Once you've added 20 elements in it, it's capacity (the underlying array size) will increase to 30. The size increase of the underlying array is actually done by constructing a new array with the new size and copying all the elements from the old array to the new one. This is a costly operation, therefore if you know your vector will grow in a fast pace, it's good to set a large increment value so that this array reallocation appears as rarely as possible.
For the 4th constructor, you basically create a vector from whatever collection you want.
Upvotes: 2
Reputation: 4397
You should use Vector(int size, int incr) when you want to control, what size for Vector will be set after it overflows.
You should use Vector(Collection c) when you want to fill it with values from another Collection.
Follow this link for further information.
Please note that in the most cases you should use ArrayList, not Vector. Vector has its method synchronized, you won't need that always.
Upvotes: 5
Reputation: 17893
Vector(int size, int incr)
incr - if the Vector gets full, by how much amount its capacity should be increase.
Vector(Collection c)
If you already have another collection (List,set) and create an initial vector by copying those collection values.
Upvotes: 2
Reputation: 12586
The third one, from the documentation:
Constructs an empty vector with the specified initial capacity and capacity increment.
Simply put, the second parameter tells by how much the capacity will increase with when the old one is reached.
And the fourth, from the documentation:
Constructs a vector containing the elements of the specified collection, in the order they are returned by the collection's iterator.
You can insert elements when you create the Vector.
Upvotes: 1
Reputation: 204756
capacityIncrement - the amount by which the capacity is increased when the vector overflows.
That means if your vector can store 10 items and you put the 10th in it then teh vector will increase size. You can tell the vector how many items more he can store from that moment on. If you have a vector where you add a lot of items then you can increase increment. then the vector does not have to increase size all the time which takes performance.
Vector(Collection c): Constructs a vector containing the elements of the specified collection, in the order they are returned by the collection's iterator.
You can put a collection of elements in the vector and use the functionality of the vector class.
Upvotes: 4