Objects read by Java vecotr must be explicitly converted before they can be correctly used.

Source: Internet
Author: User

Vector is a tool class provided by Java. util to realize the coexistence of different types of elements in a variable-length array.

Objects of the Vector class (not simple data types) not only store a column of ordered data, but also encapsulate many useful methods to operate and process the data, which is more powerful than arrays.

Suitable for vector classes:

(1) The number of objects to be processed is not fixed. All elements in the sequence are objects, or they can be expressed as objects;

(2) different classes of objects need to be combined into a data series;

(3) Frequent insertion and deletion of elements in the object sequence is required;

(4) It is often necessary to locate objects in the sequence or other search operations;

(5) transfer a large amount of data between different classes.

Vector Functions:

1. Storage of arbitrary objects

2. Basic Types of data, such as 39, cannot be stored unless the data is wrapped in the package class.

3. Its capacity can be automatically expanded as needed

4. If you do not need to expand the capacity, the efficiency of adding element methods is high.

Method:

Vector myvector = new vector () // The initial capacity is 10

Vector myvector = new vector (INT cap) // The initial capacity is cap.

Vector myvector = new vector (Collection col) // use the ** Col element for initialization. If col is an array, convert the array to a list object.

Example: vector myvector = new vector (arrays. aslist (COL ));

Void add (Object OBJ) // Add the given parameter object to the end of the original element of the vector

Boolean addall (colletion col) // Add all elements in the ** Class Object to the receiver object of this method. If the receiver's result changes, true is returned.

Int size () // returns the number of elements

Boolean isempty () // determines whether the vector is empty.

Object get (int pos) // returns the elements at the specified position

Void set (INT POs, object OBJ) // Replace the POs object in the vector with OBJ

Boolean remove (Object OBJ) // Delete the OBJ object found for the first time. If true is returned, false is returned if no object is found.

Object remove (int pos) // removes elements at a given position and returns the removed object

Boolean removeall (Collection col) // deletes all objects in the vector that appear in col. If the receiver's result changes, true is returned.

Void clear () // delete all elements of a vector

Boolean contain (Object OBJ) // determines whether the vector contains obj.

Boolean containall (Collection col) // determines whether the vector contains all elements in col.

Int indexof (Object OBJ) // returns the position where OBJ appears for the first time in the vector. If it cannot be found,-1 is returned.

Enumeration elements () // returns the enumeration object of all elements in the vector. Note that this method cannot be used in arraylist.

Iterator () // return the iterator object of all elements in the vector

Note: The object get (int pos) // read the element whose position is pos. Because it returns the object, it must be plastic (explicitly converted)

Example:

For (INT I = 0; I <v. Size (); I ++)

Custmer c = (custmer) v. Get (I); // an explicit conversion is required for each object read from v.

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.