Home>

vector is a class in the java.util package,This class implements functions similar to dynamic arrays.

Vectors are similar to arrays,Both can save a set of data (data list). But the size of the array is fixed,Once specified,Cannot be changed,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.

You can insert different types of objects anywhere in the vector,Regardless of the type of object,There is also no need to consider the capacity of the vector.

Vectors and arrays are suitable for different occasions,Generally speaking,The following situations are more suitable for using vectors:

If you need to insert and delete objects frequently,Or because the number of objects to be processed varies.

List members are all objects,Or it can be easily represented by objects.

Need to quickly determine if a particular object exists in the list,And I hope to know where the objects are stored soon.

A vector as an object provides more methods than an array,But it ’s important to note thatVectors can only store objects,Can't store simple data types directly,Therefore the following fields are suitable for using arrays:

The number of objects to be processed can be roughly determined. What you need to deal with is simple data types.

Use of vectors

Vectors must be created before use.The size of a vector is the number of elements in the vector,The capacity of a vector is the amount of memory allocated to store elements,Its size is always greater than the size of the vector.Here's how to construct a vector:

vector ();//①Create an empty vector,Initial size is 10
vector (int initialcapacity);//② Create an empty vector with an initial capacity
vector (int initialcapacity, int capacityincrement);//③Create an empty vector with an initialcapacity and an increase of capacityincrement

Using the first method, the system will automatically manage the vector.

Using the second method, an empty vector with an initial capacity (that is, the amount of data that the vector can store) of initialcapacity is created.When the data actually stored exceeds this capacity,The system will automatically expand the capacity.Doubled each time.

In the third method, an empty vector with an initial capacity of initialcapacity will be created.When the data actually stored exceeds this capacity,The system automatically expands the capacityincrement every time. If capacityincrement is 0, then double each time,.

By allocating more memory space than needed,Vectors reduce the number of necessary memory allocations.This can effectively reduce the time it takes to allocate,The amount of extra space allocated at a time will be determined by the increment specified when the vector was created.

In addition to the construction method,The vector class also provides three attribute variables,They are:

protected int capacityincrement;//When the size of the vector is insufficient,Increment size used
protected int elementcount;//Number of elements of the vector
protected object elementdata [];//Buffer used for vector member data

Once an instance of the vector class is created,You 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 common methods:

Same as array,Vector objects can also be implemented with the new operator.The statement is:

 vector vector=new vector ();
  • Previous JSP method for quickly uploading files
  • Next Java implementation of simple calculator class instance