Vectors in Java

I recently started learning Java. I studied Vectors , and I came across various Vector ad methods

Vector()

Vector (int size)

Vector (int size, int incr)

Vector (collection c)

I was able to understand the first two types, but could not understand the type of increment of the 3rd type and what and when to use the 4th type.

plz explain.

+4
source share
5 answers

capacityIncrement - the amount by which capacity increases when the vector overflows.

This means that if your vector can store 10 items, and you put the 10th in it, then the vector will increase the size. You can tell the vector how many items it can store from now on. If you have a vector into which you add a lot of objects, you can increase the increment. then the vector should not increase in size all the time, which requires performance.

Vector (Collection c): Creates a vector containing the elements of the specified collection, in the order in which they are returned by the collection iterator.

You can put a collection of elements in a vector and use the functionality of a vector class.

+4
source

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 populate it with values ​​from another collection.

Follow this link for more information.

Note that in most cases you should use an ArrayList , not a Vector . The vector is synchronized with its method, you will not always need it.

+5
source
Vector(int size, int incr) 

incr - if the vector is filled, then how much its capacity should increase.

 Vector(Collection c) 

If you already have another collection (List, set) and create an initial vector by copying these collection values.

+2
source

Vectors use a base array to store their elements. As you know, the capacity of the array is fixed. Once you said that the size of the array is 20, this will never change:

 int[] array = new int [20]; 

For the third constructor, take, for example:

 Vector v = new Vector(20, 10); 

This means that although the vector is initially empty, it has a base array of size 20. After you add 20 elements to it, its capacity (the size of the base array) will increase to 30. The increase in the size of the base array is actually done by creating a new array with new size and copy all the elements from the old array to the new one. This is an expensive operation, so if you know that your vector will grow at a fast pace, it is useful to set a large increment value so that the redistribution of this array looks as rare as possible.

For the 4th constructor, you basically create a vector from any collection you want.

+2
source

Third, from the documentation :

Creates an empty vector with a given initial power and capacity increase.

Simply put, the second parameter indicates how much capacity will increase when the old one is reached.

And fourth, from the documentation :

Creates a vector containing the elements of the specified collection in the order in which they are returned by the collection iterator.

You can insert elements when creating Vector.

+1
source

Source: https://habr.com/ru/post/1389612/


All Articles