Example of vector class usage in Java summary _java

Source: Internet
Author: User

Basic operations 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); 
   Join As String Object V1.addelement ("one"); 
   Joined as an integer object V1.addelement (integer1); 
   V1.addelement (Integer1); 
   V1.addelement ("two"); 
   V1.addelement (New Integer (2)); 
   V1.addelement (Integer1); 
   V1.addelement (Integer1); 
   Convert to string and print System.out.println ("The Vector v1 is:\n\t" +v1); 
   Inserts a new object to 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 position to the new object//The object after the specified position is later postponed V1.setelementat ("four", 2); 
   System.out.println ("The vector v1 cused method Setelmentat () is:\n\t" +v1); 
   V1.removeelement (Integer1); 
    Remove objects from Vector object V1 integer1//Because there are multiple integer1, start from scratch. 
   Find the first integer1 found in the deletion. 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 enumeration class (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 position of the object Integer1 in different directions v1.setsize (4); 
   System.out.println ("The new vector (resized the vector) is:" +V1); 

 Reset the size of the V1, extra elements are discarded}}

Run Result:

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] 


1 time-fold expansion of vertor
remember ArrayList 0.5 times times as much as a meta array each time? Vector is slightly different from ArrayList in the expansion operation

protected int capacityincrement;//is used to specify capacity

private void grow (int mincapacity) {
 //overflow-conscious code< for each expansion C3/>int oldcapacity = elementdata.length;
 int newcapacity = oldcapacity + (capacityincrement > 0)?
          capacityincrement:oldcapacity)//If capacityincrement is not specified, the capacity of the original array is the capacity
 if (newcapacity-mincapacity < 0)
  newcapacity = mincapacity;
 if (newcapacity-max_array_size > 0)
  newcapacity = hugecapacity (mincapacity);
 Elementdata = arrays.copyof (Elementdata, newcapacity);
}

A careful little partner can discover that the vector has an extra capacityincrement variable that specifies the increment for each expansion, and if you do not specify the variable, the vector can be found in the grow by default to 1 time times the size of the original array.

Thread Safety
Vertor is thread-safe!
Vertor source in another more conspicuous place is that most of the methods have synchronized keywords, we all know that this keyword is used for thread synchronization, so the vector class is thread-safe!
But even if all of its methods are modified to be synchronized, it does not mean that there is never a need for synchronization to invoke 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 () {A for
 int i=0;i <vector.size (); i++)
 {
  vector.remove (i);
 }}}
 );
 
 Thread Printthread=new Thread (new Runnable () {
 @Override public
 Void Run ()
 {A 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 will find a arrayindexoutofboundsexception anomaly, where the vector's Get,remove,size method is synchronized decorated, but in a multithreaded environment, This code is still unsafe if there is no additional synchronization at the method end, and if one thread deletes the element of ordinal I, the other thread throws the exception directly when it accesses the I, so that the code is safe and needs to be run to add a synchronized modifier.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.