Vector implements automatic growth of objects when constructing an array Vector. If no number is added, the default value is 10. 2. When a number is added, the default value is 10. When putting two numbers in, the first is the default size, and the second is the second amplification, which reallocates memory once the first number exceeds the large range
Java.util. vector provides vector classes to perform functions similar to dynamic arrays. Pointers are not a concept in the Java language, but they can greatly improve the quality of programs if used correctly and flexibly. For example, in C and C ++, so-called “dynamic arrays” are usually implemented by Pointers. To compensate for this shortcoming, Java provides a rich library of classes that programmers can easily use. The Vector class is one of them. In fact, the flexible use of arrays can also achieve the function of vector classes, but vector classes provide a large number of methods to greatly facilitate the use of users. After creating an object of vector class, you can insert objects of different classes into it at will, that is, you do not need to take into account the type and do not need to select the capacity of the vector in advance, and you can easily search. For those who do not know or do not want to define the array size in advance and need to search, insert, and delete frequently. Consider using vector classes.
Vector classes provide three constructs: public vector() public vector(int initialcapacity,int capacityIncrement) public vector(int initialcapacity)
Using the first method, the system will automatically manage the vector, while using the latter two methods. Then, 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 actual number of data stored exceeds the capacity. The system will expand the storage capacity of vector objects.
Parameter CapacityIncrement specifies the capacityincrement value for each expansion. When capacityIncrement is 0, the capacity is doubled every time, which can be used to optimize the storage. The Vector class provides a variety of methods for the user to use:
(1) public final synchronized void adddElement(Object obj) Obj can be any type of object. For the same vector object, you can also insert objects of different classes into it. But inserts should be objects, not numbers, so be careful to convert arrays to corresponding objects when inserting values. Vector v1 = new Vector(); v1.addelement (1); Integer integer1 = new Integer(1); v1.addElement(integer1); Public final synchronized void setElementAt(Object obj,int index) public final synchronized void setElementAt(Object obj,int index) Public final synchronized void insertElement(Object obj,int index) Public final synchronized void insertElement(Object obj,int index)
(1)public final synchronized void removeElement(Object obj) public final synchronized void removeElement(Object obj) (2)public final synchronized void removeAllElement(); Public fianl synchronized void removeElementAt(int index) Synchronized void removeElementAt(int index
Query search function: Public final int indexOf(Object obj) public final int indexOf(Object obj) If obj does not exist, return -1. (2)public final synchronized int indexOf(Object obj,int index) Search obj at index. (3)public final int (4) Public final synchornized int lastIndex(Object obj,int index) Obj (6) Public Final Synchornized firstElement() retrieves the first obj (6)public Final Synchornized Object in the vector Object by reverse-searching obj (5)public final Synchornized firstElement() from the subindex represented by index LastElement () gets the last obj of the vector object
// by CSDN from CSDN
Why is vector not recommended in Java today?
Since vector is thread-safe, it is inefficient, like a StringBuffer. 2. When a vector runs out of space, it doubles its capacity, whereas an ArrayList is only half as large. Easy memory allocation failure 4. Insert and delete operations can only be performed in the tail, which is inefficient
As another typical implementation class of List, Vector fully supports all functions of List. The implementation of Vector is actually very similar to the underlying implementation of ArrayList, which encapsulates an Object[]. However, Vector is a relatively old collection, which already exists in JDK1.0. It is not recommended to use this collection. The main difference between Vector and ArrayList is that Vector is thread-safe and ArrayList is non-thread-safe, but Vector has lower performance than ArrayList.