Java vector vectors

Source: Internet
Author: User

Vector vectors are a class in the Java.util package that inherits Abstractlist and implements a function similar to a dynamic array.

Vectors and arrays are similar and can hold a set of data, but the size (length) of the array is fixed, and the size of the vector is variable.

Vectors are thread-safe (the source method uses the Synchronized keyword), and vectors can only store objects.

ImportJava.util.Vector; Public classVectortest { Public Static voidMain (String []arg) {//Create vectors, vectors are created in 3, the first way the system automatically manages the vectors,//The second creates an empty vector with an initial capacity of 20, and if the stored data exceeds the capacity, the system automatically expands the capacity by a factor of//The third creates an empty vector with an initial capacity of 10, which expands by 3 lengths if the stored data exceeds the capacity .Vector v1=NewVector (); Vector v2=NewVector (20); Vector v3=NewVector (10,3); //1. Insert an object to the tail of the vector//Note that if you want to insert an integer 1, you cannot insert directly such as v1.addelement (1) is wrong, because the AddElement () method can only pass in the object, the correct wording is as follows:Integer one =NewInteger (1);        V1.addelement (one);        V1.addelement (one);        V1.addelement (one); //2. Replacing an object in a vector//The first argument is an object that replaces the vector, and the second is the object labeled int in the replacement vectorV1.setelementat ("obj", 2); //3. Inserting an object in one place//The first parameter is the inserted object, the second parameter is the insertion position, and the original object and subsequent objects are deferred sequentiallyV1.insertelementat ("obj", 2); //4. Delete the first one object found from the vectorv1.removeelement (one);        V1.remove (one); //5. Delete all objects v1.removeallelement (); //6. Delete the object that specifies the subscriptV1.removeelementat (1); //7. Start querying the object from the head of the vector and return to the first index that you have found, and you can specify to start looking for it from a subscriptV1.indexof ("obj"); V1.indexof ("Obj", 3); //8. Reverse Search from Tail    }}

Java vector vectors

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.