Vector (vector) is a class in the java.util package that implements functions similar to dynamic arrays.
Vectors are similar to arrays, and they can save a set of data (data list). However, the size of the array is fixed and cannot be changed once specified. However, vectors provide a function similar to "dynamic arrays". One of the important differences between vectors and arrays is that the capacity of vectors is variable.
Different types of objects can be inserted anywhere in the vector, without considering the type of object or the capacity of the vector.
Vectors and arrays are suitable for different occasions. Generally speaking, the following occasions are more suitable for using vectors:
If you need to frequently insert and delete objects, or because the number of objects that need to be processed is uncertain.
All list members are objects, or they can be easily represented by objects.
It is necessary to quickly determine whether a specific object exists in the list, and hope to quickly understand where the object is stored.
Vectors as an object provide more methods than arrays, but it should be noted that vectors can only store objects and cannot directly store simple data types. Therefore, the following occasions are suitable for using arrays:
Use of vectors
Vectors must be created first and then used. The size of the vector is the number of elements in the vector. The capacity of the vector is the memory size allocated to store elements, and its size is always greater than the size of the vector. Here is the constructor method of Vector:
Vector(); //① Create an empty vector with an initial size of 10Vector(int initialCapacity); //② Create an empty vector with an initial capacity of capacity Vector(int initialCapacity,int capacityIncrement); //③ Create initial Capacity to initialCapacity Empty vector with capacityIncrement
Using the first method, the system will automatically manage vectors.
Using the second method, an empty vector with the initial capacity (that is, the size of the vector that can store data) will be created. When the actual data stored exceeds this capacity, the system will automatically expand the capacity, double each time.
Using the method 3, an empty vector with initial capacity of initialCapacity will be created. When the actual data stored exceeds this capacity, the system will automatically expand capacityIncrement each time. If capacityIncrement is 0, then double each time.
By allocating more memory space than required, the vector reduces the number of memory allocations necessary. This effectively reduces the time consumed for allocation, and the number of additional spaces allocated at each time will be determined by the increment specified when the vector is created.
In addition to the construction method, the vector class also provides three attribute variables, namely:
protected int capacityIncrement; //When the vector size is insufficient, the increment size used is protected int elementCount; //The number of elements of the vector protected Object elementData[]; //The buffer used for vector member data
Once an instance of the Vector class is created, it can use its methods to perform operations such as inserting, deleting, and finding objects. Vector classes provide extremely rich methods. The following table gives some commonly used methods:
Like arrays, vector objects can also be implemented through the new operator. Its statement is:
Vector vector=new Vector();