Use of Vector

Source: Internet
Author: User
Zookeeper

Like arraylist, vector is implemented through arrays. The difference is that it supports thread synchronization, that is, only one thread can write a vector at a time point, avoid inconsistency caused by simultaneous write of multiple threads, but it is costly to implement synchronization. Therefore, it is slower to access arraylist than to access arraylist.


The differences between arraylist and vector are as follows:

  1. When the memory is insufficient, arraylist expands by 50% + by default, and vector expands by 1 times by default.
  2. Vector provides the indexof (OBJ, start) interface, which is not available in arraylist.
  3. Vector is thread-safe, but vector is not used in most cases, because thread security requires greater system overhead.

Package vectortest; import Java. util. vector; import Java. util. enumeration; public class vectortest {public static void main (string [] ARGs) {vector V1 = new vector (); integer integer1 = new INTEGER (1 ); v1.addelement ("one"); // added to the string object v1.addelement (integer1); v1.addelement (integer1); // added to the integer object v1.addelement ("two "); v1.addelement (New INTEGER (2); v1.addelement (integer1); v1.addelement (integer1); system. out. println ("the vector V1 is: \ n \ t" + V1); // convert V1 to a string and print v1.insertelementat ("three", 2 ); // Insert a new object to the specified position. The objects after the specified position are postponed to v1.insertelementat (new float (3.9), 3); system. out. println ("the vector V1 (used method insertelementat () is: \ n \ t" + V1); v1.setelementat ("four", 2 ); // set the object at the specified position as the new object system. out. println ("the vector V1 (used method setelementat () is: \ n \ t" + V1); v1.removeelement (integer1); // Delete the integer1 object from Vector object V1, because multiple integer1 exist, you can start from the beginning and delete the first integer enumeration enum1 = v1.elements (); // use the enumeration class) to obtain each element of the vector object system. out. print ("the vector V1 (used method removeelement () is:"); While (enum1.hasmoreelements () system. out. print (enum1.nextelement () + ""); system. out. println (); // query the location of the integer1 object in different directions: system. out. println ("the position of object 1 (top-to-bottom):" + v1.indexof (integer1); system. out. println ("the position of object 1 (tottom-to-top):" + v1.lastindexof (integer1); // reset the V1 size, excessive elements are discarded. v1.setsize (4); system. out. println ("the new vector (resized the vector) is:" + V1 );}}

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.