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/