Usage of Vector in Java

Source: Internet
Author: User

Arraylist is faster than vector. It is non-synchronous. If the design involves multiple threads, it is better to use vector.
The vector class provides the function of increasing arrays. As more elements are added to the array, the array becomes larger. After deleting some elements, the array becomes smaller.
Vector has three constructors,
Public vector (INT initialcapacity, int capacityincrement)
Public vector (INT initialcapacity)
Public vector ()
When a vector is run, an initial storage capacity initialcapacity is created. The storage capacity is incremental growth defined by the capacityincrement variable. The initial storage capacity and capacityincrement can be defined in the vector constructor. The second constructor only creates the initial storage capacity. The third constructor does not specify the initial storage capacity or capacityincrement.
The access method provided by the vector class supports operations similar to Array Operations and vector size operations. Operations similar to arrays allow adding, deleting, and inserting elements in vectors. They also allow testing the content of the vector and retrieving the specified elements. The size-related operations allow determining the byte size and the number of elements in the vector.
Examples of adding, deleting, and inserting vectors that are frequently used:
Addelement (Object OBJ)
Add the component to the end of the vector, and increase the size by 1. The vector capacity is 1 larger than the previous one.
 
Insertelementat (Object OBJ, int index)
Add the component to the specified index, and move the subsequent content to one unit.
 
Setelementat (Object OBJ, int index)
Add the component to the specified index. The content here is replaced.

Removeelement (Object OBJ) removes the content of the component contained in the vector.
Removeallelements () removes all components from the vector. The vector size is 0.
For example:
Import java. Lang. system;
Import java. util. vector;
Import java. util. enumeration;

Public class vectortest {
Public static void main (string ARGs [])
{
Vector v = new vector ();
V. addelement ("one ");
System. Out. println (v );
V. addelement ("two ");
System. Out. println (v );
V. addelement ("three ");
System. Out. println (v );
V. insertelementat ("zero", 0 );
System. Out. println (V );;
V. insertelementat ("Oop", 3 );
System. Out. println (v );
V. setelementat ("three", 3 );
System. Out. println (v );
V. setelementat ("four", 4 );
System. Out. println (v );
V. removeallelements ();
System. Out. println (v );
}
}

Changes in vector:

[One, Two]
[One, Two, Three]
[Zero, one, two, three]
[Zero, one, two, oop, three]
[Zero, one, two, three, three]
[Zero, one, two, three, four]

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.