The vector class implements a dynamic array. and ArrayList and similar, but the two are different:
- The vector is accessed synchronously.
- Vectors contain a number of traditional methods that do not belong to the set framework.
Vectors are primarily used in cases where the size of an array is not known beforehand, or if only an array of size can be changed.
The vector class supports 4 methods of construction.
The first construction method creates a default vector with a default size of 10:
Vector ()
The second construction method creates a vector of the specified size.
Vector (int size)
The third construction method creates a vector of the specified size, and the increment is specified with incr. Increments represent the number of elements each increment of the vector.
Vector (int size,int incr)
The construction method in the IV creates a vector that contains the C elements of the collection:
Vector (Collection c)
In addition to the methods inherited from the parent class, the vector also defines the following methods:
Serial Number |
Method Description |
1 |
void Add (int index, Object Element) Inserts the specified element at the specified position in this vector. |
2 |
Boolean Add (Object o) Adds the specified element to the end of this vector. |
3 |
Boolean AddAll (Collection c) Adds all the elements in the specified Collection to the end of this vector, adding the elements in the order returned by the specified Collection iterator. |
4 |
Boolean addall (int index, Collection c) Inserts all the elements in the specified Collection into this vector at the specified location. |
5 |
void AddElement (Object obj) Adds the specified component to the end of this vector, increasing its size by 1. |
6 |
int capacity () Returns the current capacity of this vector. |
7 |
void Clear () Removes all elements from this vector. |
8 |
Object Clone () Returns a copy of the vector. |
9 |
Boolean contains (Object elem) Returns true if this vector contains the specified element. |
10 |
Boolean containsall (Collection c) Returns true if this vector contains all the elements in the specified Collection. |
11 |
void Copyinto (object[] anarray) Copies the component of this vector to the specified array. |
12 |
Object elementat (int index) Returns the component at the specified index. |
13 |
Enumeration elements () Returns an enumeration of the components of this vector. |
14 |
void ensurecapacity (int mincapacity) Increase the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity parameter. |
15 |
Boolean equals (Object O) Compares the equality of the specified object to this vector. |
16 |
Object firstelement () Returns the item at the first component of this vector (at index 0). |
17 |
Object get (int index) Returns the element at the specified position in the vector. |
18 |
int Hashcode () Returns the hash code value for this vector. |
19 |
int indexOf (Object elem) Returns the index of the specified element that first appears in this vector, or 1 if the vector does not contain the element. |
20 |
int indexOf (Object elem, int index) Returns the index of the specified element that first appears in this vector, a forward search from index, or 1 if the element is not found. |
21st |
void Insertelementat (Object obj, int index) Inserts the specified object as a component in this vector at the specified index. |
22 |
Boolean IsEmpty () Tests whether this vector does not contain components. |
23 |
Object lastelement () Returns the last component of this vector. |
24 |
int lastIndexOf (Object elem) Returns the index of the last occurrence of the specified element in this vector, or 1 if the vector does not contain the element. |
25 |
int lastIndexOf (Object elem, int index) Returns the index of the specified element in the last occurrence of this vector, searching backwards from index, or 1 if the element is not found. |
26 |
Object Remove (int index) Removes the element at the specified position in this vector. |
27 |
Boolean remove (Object o) Removes the first occurrence of the specified element in this vector, and if the vector does not contain the element, the element remains unchanged. |
28 |
Boolean RemoveAll (Collection c) Removes all elements contained in the specified Collection from this vector. |
29 |
void Removeallelements () Removes all components from this vector and sets their size to zero. |
30 |
Boolean removeelement (Object obj) Removes the first (least indexed) occurrence of a variable from this vector. |
31 |
void Removeelementat (int index) Deletes the component at the specified index. |
32 |
protected void RemoveRange (int fromIndex, int toindex) Removes all elements whose indexes are between fromIndex (inclusive) and Toindex (not included) from this list. |
33 |
Boolean retainall (Collection c) Only the elements contained in the specified Collection are preserved in this vector. |
34 |
Object Set (int index, object element) Replaces the element at the specified position in this vector with the specified element. |
35 |
void Setelementat (Object obj, int index) Sets this vector to the specified object at the component at index. |
36 |
void setSize (int newSize) Sets the size of this vector. |
37 |
int size () Returns the number of components in this vector. |
38 |
List sublist (int fromIndex, int toindex) Returns a partial view of this List, with elements ranging from fromIndex (including) to Toindex (not included). |
39 |
Object[] ToArray () Returns an array that contains all the elements in the vector that are stored in the proper order. |
40 |
Object[] ToArray (object[] a) Returns an array containing all the elements in the proper order in this vector; the run-time type of the returned array is the type of the specified array. |
41 |
String toString () Returns a string representation of this vector that contains a string representation of each element. |
42 |
void TrimToSize () Fine-tune the capacity of this vector so that it is equal to the current size of the vector. |
Instance
The following program illustrates several of the methods supported by this collection:
ImportJava.util.*; Public classVectordemo { Public Static voidMain (String args[]) {//Initial size is 3, increment is 2Vector v =NewVector (3, 2); System.out.println ("Initial Size:" +v.size ()); System.out.println ("Initial Capacity:" +v.capacity ()); V.addelement (NewInteger (1)); V.addelement (NewInteger (2)); V.addelement (NewInteger (3)); V.addelement (NewInteger (4)); System.out.println ("Capacity after four additions:" +v.capacity ()); V.addelement (NewDouble (5.45)); System.out.println ("Current Capacity:" +v.capacity ()); V.addelement (NewDouble (6.08)); V.addelement (NewInteger (7)); System.out.println ("Current Capacity:" +v.capacity ()); V.addelement (NewFloat (9.4)); V.addelement (NewInteger (10)); System.out.println ("Current Capacity:" +v.capacity ()); V.addelement (NewInteger (11)); V.addelement (NewInteger (12)); System.out.println ("First element:" +(Integer) v.firstelement ()); System.out.println ("Last element:" +(Integer) v.lastelement ()); if(V.contains (NewInteger (3)) ) System.out.println ("Vector contains 3."); //enumerate the elements in the vector.Enumeration Venum =v.elements (); System.out.println ("\nelements in Vector:"); while(Venum.hasmoreelements ()) System.out.print (Venum.nextelement ()+ " "); System.out.println (); }}
The results of the above example compilation run as follows:
Initial size:03557913. Elements in Vector:1 2 3 4 5.45 6.08 7 9.4 10 11 12
Java Vector Class