The vector class in Java is detailed with method examples

  • 2020-07-21 07:33:50
  • OfStack

Basic operation example

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 a string object  
 v1.addElement("one"); 
 // Join the for integer The object of  
 v1.addElement(integer1); 
 v1.addElement(integer1); 
 v1.addElement("two"); 
 v1.addElement(new Integer(2)); 
 v1.addElement(integer1); 
 v1.addElement(integer1); 
 // Converts to a string and prints  
 System.out.println("The Vector v1 is:\n\t"+v1); 
 // Inserts a new object into the specified location  
 v1.insertElement("three",2); 
 v1.insertElement(new Float(3.9),3); 
 System.out.println("The Vector v1(used method 
 insertElementAt()is:\n\t)"+v1); 
 // Sets the object at the specified location to the new object  
 // Objects after the specified position are deferred in turn  
 v1.setElementAt("four",2); 
 System.out.println("The vector v1 cused method setElmentAt()is:\n\t"+v1); 
 v1.removeElement(integer1); 
 // Slave vector object v1 Delete object in integer1 
 // Because there are multiple integer1, So start from scratch.  
 // Look for the deleted item 1 a integer1. 
 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(); 
 // Using enumerated classes (Enumeration) Method to get each element of a 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)); 
 // Find the object in a different direction integer1 The position of the  
 v1.setSize(4); 
 System.out.println("The new Vector(resized the vector)is:"+v1); 
 // To reset v1 The size of the extra elements is discarded  
 } 
} 

Operation results:


E:\java01>java VectorApp 
The vector v1 is:[one,1,1,two,2,1,1] 
The vector v1(used method insetElementAt()) 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] 

A one-fold expansion of Vertor

Remember that ArrayList expands to 0.5 times the size of a tuple? Vector is slightly different from ArrayList in its capacity expansion operation


protected int capacityIncrement;// Used to specify the capacity of each expansion 
private void grow(int minCapacity) {
 // overflow-conscious code
 int oldCapacity = elementData.length;
 int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
   capacityIncrement : oldCapacity);// If you don't specify capacityIncrement , the default 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);
}

If you are careful, you will notice that there is an extra variable in Vector, which is used to specify the increment of each expansion. If you do not specify this variable, you will find that Vector expands to twice the size of the original array by default

Thread safety

Vertor is thread safe!

Another notable feature in the Vertor source code is that most methods have the synchronized keyword, which is known to be used for thread synchronization, so the Vector class is thread-safe!

But even if all of its methods are decorated to be synchronized, that doesn't mean it will never need to be called:


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); 
 }
}

When you run this code for a short period of time you'll see that there is ArrayIndexOutOfBoundsException Exception, here Vector的get,remove,size Methods though there are synchronized In a multithreaded environment, this code is still unsafe without additional synchronization measures at the method end. If one thread removes elements of the number i and another thread accesses the i, the exception will be thrown directly. Therefore, it is necessary to make this code safe again run I'm going to add to that synchronized Modification.

I hope this vector class helped you with an example article


Related articles: