[Java learning notes] use of vector classes

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

Import java. util. vector;
Public class collectionone {
Public static void main (string Arg []) {
New collectionone ();
}
Collectionone (){
Vector <Object> VEC = new vector <Object> ();
VEC. Add ("zero ");
VEC. Add ("one ");
VEC. Add ("two ");
VEC. Add ("three ");
VEC. Add ("four ");
String STR = (string) Vec. Get (2 );
System. Out. println ("the index of 2:" + Str );
}
}

Java provides a Vector class vector in the Java. util package, which can store uncertain elements and dynamically expand as needed. The differences between vectors and arrays are as follows:

1. Java Arrays can store array elements of any type, including numerical and all class types.

2. Java vectors can only store instances of object classes.

You can relocate and adjust your capacity. Due to lack of time-saving, the capacity of a vector is doubled and exponential growth occurs once. Therefore, you should specify the capacity increment as the second parameter of the vector constructor to create a vector:

Vector itemsordered = new vector (3.10); in this way, each time a vector is relocated, its capacity is increased by 10 elements.

V. Size () returns the number of elements in the vector, which is equivalent to Arry. Length ();

If the vector size has reached its permanent size, you can use the tirmtosize method to adjust the memory block size to the vector element that can accommodate the current number.

A convenient [] syntax cannot be used in a vector, but elements must be accessed or modified using the elementat and setslementat methods.

Array: x = A [I] (ACCESS)

A [I] = x; (modify)

Vector: x = V. elementat (I)

V. setelementat (X. I)

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

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.