java.util.vector provides vector classes (vectors) to implement dynamic array-like functions. There is no concept of pointers in Java language, but using pointers correctly and flexibly can greatly improve the quality of the program. For example, the so-called "dynamic array" in c and c++ are generally implemented by pointers. In order to make up for this shortcoming, Java provides a rich class library for easy use by programmers, and vector class is one of them. In fact, the flexibility of using arrays can also complete the functions of vector classes, but the large number of methods provided in vector classes greatly facilitate users' use.
After creating an object of a vector class, you can insert objects of different classes into it at will, that is, you do not need to consider the type or the capacity of the pre-selected vector, and can easily search. For situations where you do not know or are unwilling to pre-defined array size, and need to frequently search, insert, and delete work. You can consider using vector classes.
The Vector class implements a dynamic array. It is similar to ArrayList, but the two are different:
The Vector class supports 4 construction methods.
1. The first constructor creates a default vector with a default size of 10:
Vector()
2. The second construction method creates a vector of a specified size.
Vector(int size)
3. The third construction method creates a vector of a specified size, and the increment is specified by incr. The increment represents the number of elements that the vector increases at each time.
Vector(int size,int incr)
4. The fourth constructor creates a vector containing the set c elements:
Vector(Collection c)
The system will automatically manage the vectors using the first method, if the latter two methods are used. The system will set the capacity of the vector object (that is, the size of the data that the vector object can store) according to the parameters, and when the number of data actually stored exceeds the capacity. The system will expand the storage capacity of vector objects.
The parameter capacityincrement gives the expansion value for each expansion. When capacityincrement is 0, the expansion will not be doubled. This function can be used to optimize storage. Various methods are provided in the Vector class to facilitate users' use:
Insert function:
(1) public final synchronized void adddElement(Object obj)
Insert obj into the tail of the vector. obj can be any type of object. For the same vector object, different types of objects can also be inserted into it. But the insert should be an object rather than a numeric value, so when inserting a numeric value, you should pay attention to converting the array into the corresponding object.
For example: When inserting an integer 1, do not call v1.addElement(1). The correct method is:
Vector v1 = new Vector(); Integer integer1 = new Integer(1); v1.addElement(integer1);
(2) public final synchronized void setElementAt(Object obj,int index)
Set the object at index to obj, and the original object will be overwritten.
(3) public final synchronized void insertElement(Object obj,int index)
Insert obj at the position specified in the index, and the original object and the subsequent objects will be postponed in turn.
Delete function:
(1) public final synchronized void removeElement(Object obj)
Delete obj from the vector. If there are multiple existences, start with the vector header and delete the first vector member found with the same as obj.
(2) public final synchronized void removedAllElement();
Delete all objects in vector
(3) public fianl synchronized void removeElementAt(int index)
Delete the object where the index refers to
Query search function:
(1) public final int indexOf(Object obj)
Start searching for obj from the vector header and return the subscript corresponding to the first obj encountered. If this obj does not exist, return -1.
(2) public final synchronized int indexOf(Object obj,int index)
Start searching for obj from the index indicated by index.
(3) public final int lastindexOf(Object obj)
Starting inverse search for obj from the tail of the vector.
(4) public final synchornized int lastIndex(Object obj,int index)
Search obj in reverse from the end to the head from the subscript indicated by index.
(5) public final synchornized firstElement()
Get the first obj in a vector object
(6) public final synchornized Object lastElement()
Get the last obj of the vector object
Example
The following program explains several methods supported by this collection:
import java.util.*;public class VectorDemo { public static void main(String args[]) { // initial size is 3, increment is 2 Vector v = new Vector(3, 2); System.out.println("Initial size: " + v.size()); System.out.println("Initial capacity: " + v.capacity()); v.addElement(new Integer(1)); v.addElement(new Integer(2)); v.addElement(new Integer(3)); v.addElement(new Integer(4)); System.out.println("Capacity after four additions: " + v.capacity()); v.addElement(new Double(5.45)); System.out.println("Current capacity: " + v.capacity()); v.addElement(new Double(6.08)); v.addElement(new Integer(7)); System.out.println("Current capacity: " + v.capacity()); v.addElement(new Float(9.4)); v.addElement(new Integer(10)); System.out.println("Current capacity: " + v.capacity()); v.addElement(new Integer(11)); v.addElement(new Integer(12)); System.out.println("First element: " + (Integer)v.firstElement()); System.out.println("Last element: " + (Integer)v.lastElement()); if(v.contains(new Integer(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 above example compilation and operation results are as follows:
Initial size: 0Initial capacity: 3Capacity after four additions: 5Current capacity: 5Current capacity: 7Current capacity: 9First element: 1Last element: 12Vector contains 3.Elements in vector:1 2 3 4 5.45 6.08 7 9.4 10 11 12