Detailed explanation of the use of Vector in Java (forwarding)


Vector implements an auto-growing array of objects. 
java.util.vector provides a vector class (Vector) to implement functions similar to dynamic arrays. 
After creating an object of the vector class, you can insert objects of different classes into it at will, that is, you don't need to consider the type or the capacity of the vector in advance, and you can easily find it.

For situations where you don't know or don't want to predefine the size of the array in advance, and you need to search, insert, and delete frequently, you can consider using the vector class.

The vector class provides three constructors: 

public vector() 
public vector(int initialcapacity,int capacityIncrement) 
public vector(int initialcapacity)

Using the first method, the system will automatically manage the vector. 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 parameter, initialcapacity. When the number exceeds the capacity. The system will expand the vector object storage capacity.

The parameter capacityincrement gives the augmentation value for each augmentation. When capacityincrement is 0, it is doubled each time. This function can be used to optimize storage.

Various methods are provided in the Vector class for the convenience of users:

Insertion function: 
(1) public final synchronized void adddElement(Object obj) 
         inserts obj into the tail of the vector. obj can be any type of object. For the same vector object, objects of different classes can also be inserted into it. But the object should be inserted instead of the value, so when inserting the value, pay attention to converting the array into the corresponding object. 
          For example: when you want to insert the integer 1, do not call v1.addElement(1) directly, 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 insertElementAt(Object obj, int index) 
    inserts obj at the position specified by index, and the original object and subsequent objects are sequentially extended.

Deletion function: 
(1) public final synchronized void removeElement(Object obj)
    deletes obj from the vector. If there are more than one, try starting from the head of the vector and delete the first vector member that is found to be the same as obj. 

(2) public final synchronized void removeAllElement(); 
     delete all objects of the vector 

(3) public fianl synchronized void removeElementAt(int index) 
     deletes the object pointed to by index

Query search function: 
(1) public final int indexOf(Object obj) 
searches for obj from the head of the vector, and returns the subscript corresponding to the first obj encountered. If there is no such obj, it returns -1. 

(2) public final synchronized int indexOf(Object obj, int index) 
starts searching for obj from the subscript represented by index. 

(3) public final int lastindexOf(Object obj) 
reverse search obj from the tail of the vector. 

(4) public final synchornized int lastIndex(Object obj, int index) 
searches obj in reverse from the end to the beginning of the subscript indicated by index. 

(5) public final synchornized firstElement() 
gets the first obj in the vector object 

(6) public final synchornized Object lastElement() 
gets the last obj of the vector object

Other functions:
(1) public final int size(); 
   This method is used to obtain the number of vector elements. Their return value is the actual number of elements in the vector, not the vector capacity. The method capacity() can be called to get the capacity value.

(2) public final synchronized void setSize(int newsize); 
   This method is used to define the size of the vector. If the number of existing members of the vector object has exceeded the value of newsize, the excess elements will be lost. 

An object of the Enumeration class defined in the program Enumeration is an interface class in java.util. 

(3) public final synchronized Enumeration elements(); 
   This method maps the vector object to an enumeration type. There are also such methods in other classes in the java.util package, so that users can obtain the corresponding enumeration type.

   Encapsulates methods for enumerating data collections in Enumeration. 
    The method hasMoreElement() to determine whether there are other elements in the collection.

        method nextElement() to get the next element

The following code traverses the Vector with hasMoreElement() and nextElement()

copy code
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class TestVector {
    public void test01() {
        Vector<String> hs = new Vector<String>();
        hs.add("aa");
        hs.add("bb");
        hs.add("aa");
        hs.add("cc");
        hs.add("aa");
        hs.add("dd");
        printSet2 (hs);
    }

    public void printSet(List hs) {
        Iterator iterator = hs.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    public void printSet2(Vector<String> hs) {
        Enumeration<String> elements = hs.elements();
        while (elements.hasMoreElements()) {
            System.out.println(elements.nextElement());
        }
    }

    public static void main(String[] args) {
        new TestVector().test01();
    }
}

copy code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326352767&siteId=291194637