Basic operation examples
VectorApp.java
import java.util.Vector; import java.lang.*; import java.util.Enumeration; public class VectorApp { public static void main(String args[]) { Vector v1 = new Vector(); Integer integer1= new Integer(1); //Add as string object v1.addElement("one"); //Add as integer object v1.addElement(integer1); v1.addElement(integer1); v1.addElement("two"); v1.addElement(new Integer(2)); v1.addElement(integer1); v1.addElement(integer1); //Convert to a string and print System.out.println("The Vector v1 is:/n/t"+v1); //Insert a new object to the specified position v1.insertElement("three",2); v1.insertElement(new Float(3.9),3); System.out.println("The Vector v1(used method insertElementAt() is:/n/t)"+v1); //Set the object at the specified position as a new object//The objects after the specified position are extended in sequence v1.setElementAt("four",2); System.out.println("The vector v1 cused method setElmentAt() is:/n/t"+v1); v1.removeElement(integer1); //Delete the object integer1 from the vector object v1 //Since there are multiple integer1, start from scratch. //Find to delete the first integer1 found. Enumeration enum = v1.elements(); System.out.println("The vector v1 (used method removeElememt()is"); while(enum.hasMoreElements()) System.out.println(enum.nextElement()+""); System.out.println(); //Use the method of the enumeration class (Enumeration) to obtain each element of the vector object. System.out.println("The position of Object1(top-to-botton):"+v1.indexOf(integer1)); System.out.println("The position of Object1(tottom-to-top):"+v1.lastIndexOf(integer1)); //Search the position where the object integer1 is in different directions v1.setSize(4); System.out.println("The new Vector(resized the vector)is:"+v1); //Reset the size of v1, and the excess elements are discarded} } Running results:
E:/java01>java VectorApp The vector v1 is:[one,1,1,two,2,1,1] The vector v1(used method insertElementAt()) is: [one,1,three,3.9,1,two,2,1,1] The vector v1(used method setElementAt()) is: [one,1,four,3.9,1,two,2,1,1] The vector v1(useed method removeElement()) is: one four 3.9 1 two 2 1 1 The position of object1(top-to-botton):3 The position of object1(botton-to-top):7 The new Vector(resized the vector) is: [one,four,3.9,1]
Vertor's 1x expansion
Remember if ArrayList is expanded to 0.5 times the metaarray each time? Vector is slightly different from ArrayList when performing capacity expansion operations
protected int capacityIncrement;//Use to specify the capacity for each expansion private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity);//If capacityIncrement is not specified, the default capacity expansion capacity is the capacity of the original array if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity);}Careful friends can find that there is an additional capacityIncrement variable in Vector, which is used to specify the increment of each expansion. If this variable is not specified, you can find in the grow that the Vector expands the capacity by default to 1 times the original array.
Thread safety
Vertor is thread safe!
Another more conspicuous thing in Vertor source code is that most methods have synchronized keyword. Everyone knows that this keyword is used for thread synchronization, so the Vector class is thread-safe!
But even if all its methods are modified to be synchronous, it does not mean that there is no need for synchronization when calling it:
private static Vector<Integer> vector=new Vector<Integer>();public static void main(String[] args) { while(true) { for(int i=0;i<10;i++) { vector.add(i); } Thread removeThread=new Thread(new Runnable(){ @Override public void run() { for(int i=0;i<vector.size();i++) { vector.remove(i); } } }); Thread printThread=new Thread(new Runnable(){ @Override public void run() { for(int i=0;i<vector.size();i++) { System.out.println(vector.get(i)); } } }); removeThread.start(); printThread.start(); while(Thread.activeCount()>20); }}After running this code for a short period of time, you will find an ArrayIndexOutOfBoundsException exception. Although the get, remove, and size method of Vector here has synchronized modification, in a multi-threaded environment, if no additional synchronization measures are taken at the method side, this code is still unsafe. If one thread deletes the element with serial number i, and another thread accesses this i, it will directly throw the exception. Therefore, to ensure the safety of this code, it also requires the synchronized modification to be added to run.