A detailed explanation of the application of vectors (vector) in Java programming _java

Source: Internet
Author: User
Tags arrays

A vector is a class in a java.util package that implements a function similar to a dynamic array.

Vectors and arrays are similar, you can save a set of data (a list of data). But the size of the array is fixed, and once specified, it cannot be changed, whereas vectors provide a function similar to "dynamic array", one of the important differences between vectors and arrays is that the capacity of vectors is variable.

You can insert different types of objects at any point in the vector, regardless of the type of object, or the capacity of the vector.

Vectors and arrays are suitable for different situations, and in general, the following situations are more suitable for using vectors:
If you need to make frequent insertions and deletions of objects, or because the number of objects you need to handle is variable.
List members are all objects, or they can be easily represented by objects.
You need to quickly determine whether a particular object exists in the list, and you want to quickly understand where the object is stored.

Vectors provide more methods than arrays as an object, but note that vectors can only store objects and cannot store simple data types directly, so the following scenarios apply to using arrays:

    • The number of objects you need to process is roughly certain.
    • What you need to handle is a simple data type.

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 allocated to store the memory size of the element, its size is always greater than the size of the vector. Here's how vectors are constructed:

Vector (); ① creates an empty vector, the initial size is vector
(int initialcapacity), and//② creates a vector vector with an initial capacity of capacity
(int initialcapacity,int Capacityincrement); ③ create an empty vector with an initial capacity of initialcapacity and an increment of capacityincrement


The ① system automatically manages vectors using the first method.

Using the ② method, an initial capacity (that is, the size of the vector to store the data) is created as an empty vector of initialcapacity, and when the actual data is exceeded, the system automatically expands the capacity by one time.

Using the ③ method, an empty vector with an initial capacity of initialcapacity is created, and the system automatically expands capacityincrement each time when the actual data stored exceeds that capacity. If the capacityincrement is 0, then one more times each time.

By allocating more memory space than needed, vectors reduce the number of memory allocations that are required. This can effectively reduce the time spent allocating, and the amount of extra space allocated 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 property variables, respectively:

protected int capacityincrement; When the vector size is insufficient, the increment size used is
protected int elementcount;//vector element number
protected Object elementdata[];//buffer used for vector member data


Once you create an instance of a vector class, you can use its methods to perform operations such as inserting, deleting, and finding objects, and the vector class provides an extremely rich method, and the following table gives some common methods:



As with arrays, vector objects can also be implemented by the new operator. The statement is:

  Vector vector=new vector ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.