Java knows how many () Java vectors (vector) and their applications

Source: Internet
Author: User

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

Vectors and arrays are similar and can hold a set of data (data lists). But the size of the array is fixed, and once specified, it cannot be changed, while the vector provides 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 anywhere in the vector, regardless of the type of the object or the capacity of the vector.

Vector and array are suitable for different occasions, in general, the following situations are more suitable for using vectors:

    • If you need to insert and delete objects frequently, or because the number of objects you need to work with is variable.
    • List members are all objects, or they can be easily represented by objects.
    • You need to quickly determine if a particular object exists in the list, and you want to quickly learn where the object is stored.


Vectors provide more than an array of methods, but it is important to note that vectors can only store objects and cannot directly store simple data types, so the following are suitable for working with arrays:

    • The number of objects that need to be processed can be roughly determined.
    • What you need to deal with is a simple data type.
Use of vectors

Vectors must be created before use, the size of the vector is the number of elements in the vector, the capacity of the vector is allocated to store the element's memory size, its size is always greater than the size of the vector. Here is how the Vector is constructed:

 1  Vector (); // ① creates an empty vector with an initial size of ten  2  Vector (int  initialcapacity); // ② creates an empty vector with an initial capacity of capacity  3  Vector (int  Initialcapacity,int  capacityincrement); // ③ creates an empty vector  
with an initial capacity of initialcapacity and an increment of capacityincrement

The

system automatically manages the vectors using the ① method. The

uses the ② method to create an initial capacity (that is, the size of the vector to store data) as an empty vector of  initialcapacity, and when the actual data exceeds that capacity, the system automatically expands the capacity by one more time. The

uses the ③ method to create an empty vector with an initial capacity of  initialcapacity, and the system automatically expands the capacityincrement each time the data is actually stored beyond that capacity. If the capacityincrement is 0, then increase by one more times each time.

by allocating more memory space than required, vectors reduce the number of required memory allocations. This effectively reduces the time spent on allocations, and the amount of extra space allocated each time is determined by the increment specified when the vector was created.

In addition to constructing methods, the vector class provides three property variables, namely:

1 protected int // when the vector size is insufficient, the increment size used 2 protected int // number of elements in a vector 3 protected // buffer used for vector member data

Once you have created an instance of the vector class, you can use its methods to perform operations such as inserting, deleting, and finding objects, and the vector classes provide a very rich approach, and the following table shows some common methods:

Method function
void AddElement (Object element) Increments the given object element to the end of the vector
int capacity () return vector capacity
Boolean contains (Object element) Returns true if the vector contains element, otherwise false
void Copyinto (Object array[]) Copies a vector element to a specified array
Synchronized Object elementat (int index) Returns the element that specifies the subscript, throws an Arrayindexoutofboundsexecption exception if the subscript is illegal
void ensurecapacity (int size) Set the minimum capacity of a vector to size
Synchronized Object firstelement () Returns the first element of the vector, if the vector is empty, throws a Nosuchelementexception exception
int indexOf (Object Element) Returns the subscript for element if the object does not exist return-1
int indexOf (Object element,int start) Searches for a vector from the specified position (start), returns the subscript value corresponding to the object, if no return-1 is found
void Insertelementat (Object obj,int index) Inserts the given object at the specified subscript
Boolean IsEmpty () Returns true if the vector does not include any elements, otherwise false
Synchronized Object lastelement () Returns the last element of the vector, if the vector is empty, throws a Nosuchelementexception exception
int lastIndexOf (Object Element) Searches forward vectors from the end of a vector, returning the object's subscript value
int lastIndexOf (Object element,int start) Searches forward from the specified position vector, returns the subscript value of the given object, if no return-1 is found
void Removeallelements () Delete all objects in the vector, and convert to empty vectors
Boolean removeelement (Object Element) Removes the specified object element from the vector, if the given object is saved more than once in the vector, only its first instance is deleted, and if the deletion succeeds, returns True if the object is not found, false
void Removeelementat (int index) Delete the element at the position specified by index
void Setelementat (Object obj,int index) Stores the given object at the given subscript, the original object at the subscript is lost
void setSize (int size) Sets the number of elements in the vector to size, and if the new length is less than the original length, the element is lost, and if the new length is greater than the original length, then the null element is added later
int size () Returns the number of current elements in a vector
String toString () Convert a vector to a string
void TrimToSize () Sets the capacity of the vector to be equal to the number of currently owned elements

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

1     Vector vector=new vector ();
Series Articles:Java know how much (top)Java know how much (medium)

Java knows how many () Java vectors (vector) and their applications

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.