The detailed use of vectors in Java.util.vector

Source: Internet
Author: User
Tags addall

ArrayList will be faster than the vector, he is non-synchronous, if the design involves multi-threading, or with a vector better import java.util.*;

/** * Demonstrates the use of vectors. This includes the creation of vectors, adding elements to vectors, removing elements from vectors, * Counting the number of elements in a vector, and traversing elements in a vector. */

public class vectordemo{public static void Main (string[] args) {

Vector creation//Use vector construction method to create vector v = new vector (4);

Add elements to vector//Use the Add method to add element V.add directly ("Test0"); V.add ("Test1"); V.add ("Test0"); V.add ("Test2"); V.add ("Test2");

Removes the element v.remove ("Test0") from the vector; Deletes an element of the specified content v.remove (0); Delete an element by index number

Gets the number of elements already in the vector int size = v.size (); SYSTEM.OUT.PRINTLN ("Size:" + size);

Traverse the elements in the vector for (int i = 0;i < V.size (); i++) {System.out.println (V.get (i));}} The-------------Vector class provides the ability to implement a growing array, and as more elements are added to it, the array becomes larger. After you delete some elements, the array becomes smaller.          Vector has three constructors, public vector (Int. initialcapacity,int capacityincrement) public vector (int initialcapacity) The public vector () vector runtime creates an initial storage capacity of initialcapacity, which is the incremental growth defined by the capacityincrement variable. The initial storage capacity and capacityincrement can be defined in the vector's constructor. The second constructor creates only the initial storage capacity.   The third constructor does not specify either the initial storage capacity or the capacityincrement. The vector class provides access methods that support similar array operations and operations related to vector size. An array-like operation allows the addition, deletion, and insertion of elements in the vector.   They also allow you to test the contents of the vector and retrieve the specified elements, and the size-dependent operations allow you to determine the size of the bytes and the number of elements in the vector.     Now for the frequently used vector increase, delete, interpolation function Example Description: AddElement (Object obj) to add the component to the tail of the vector, while the size plus 1, the vector capacity than before 1 insertelementat (Object obj, int index)   Add the component to the index at which it was set, then move backwards by 1 units setelementat (Object obj, int index) adds the component to the index, where the contents are replaced.   Removeelement (Object obj) removes the contents of this component from the vector. Removeallelements () Moves all the components in the vector to a vector size of 0.

For example:

Import Java.lang.System;

Import Java.util.Vector;

Import java.util.Emumeration;

public class avector{

public static void Main (String args[]) {0.Vector v=new Vector ();   1. V.addelement ("one");   2. AddElement ("both");   3. V.addelement ("three");   4. V.insertelementat ("Zero", 0);   5. V.insertelementat ("Oop", 3);   6. V.setelementat ("three", 3);   7. V.setelementat ("Four", 4);   8. v.removeallelements (); }} Vector changes: 1. One 2. One 3. One 4. Zero 5.zero 6. Zero 7.       Zero 8.     One one one three three oop three three three three four In addition, Vector plays an important role in parameter transfer. There is a canvas (canvas) and a (panel) in the applet, and the Panel has the information that the user wants to input, which passes the parameters to the canvas, using an interface (Interface) in Java, and a vector in the interface To pass these parameters.   In addition, this method can be used to pass a class to another class parameter.     For example: Import Java.util.Vector interface codeselect{vector codeselect=new vector (); Display mathematical information vector (0) deposit student number vector (1) deposit to subject In the panel, when the user chooses what they want in TextField and choice, the program passes the value to vector vectors through the event response.

Synchronization is a big problem, especially in multi-threading, and processes, so when we operate on an array at the same time in multi-threading, it is a good choice to support a vector that is synchronized, typically when multiple elements need to exist in a collection. The Java.util class vector<e> Boolean add (E O) appends the specified element to the end of this vector. void Add (int index, E Element) inserts the specified element at the specified position in this vector. Boolean AddAll (collection<? extends e> c) appends all elements in the specified Collection to the end of this vector, appending them in the order returned by the iterator for the specified collection. Boolean addall (int index, COLLECTION&LT;? extends e> c) inserts all the elements in the specified Collection into this vector at the specified location. void AddElement (E obj) adds the specified component to the end of this vector, increasing its size by 1. int capacity () returns the current capacity of this vector. void Clear () Removes all elements from this vector. Object Clone () returns a copy of the vector. The Boolean contains (object Elem) tests whether the specified object is a component in this vector. Boolean containsall (collection<?> c) returns True if this vector contains all the elements in the specified Collection. void Copyinto (object[] anarray) Copies the components of this vector into the specified array. E elementat (int index) returns the component at the specified index. Enumeration<e> elements () returns an enumeration of the components of this vector. void ensurecapacity (int mincapacity) increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity parameter. The Boolean equals (object O) compares the equality of the specified object to this vector. E firstelement () returns the first component of this vector (the item at index 0). E get (int index) returns the element at the specified position in the vector. int Hashcode () returns the hash code value for this vector. InchT IndexOf (Object elem) searches for the first occurrence of a given parameter, using the Equals method to test for equality. int indexOf (Object elem, int index) searches for the first occurrence of a given parameter, starts the search at index, and tests its equality using the Equals method. void Insertelementat (E obj, int index) inserts the specified object as a component in this vector at the specified index. Boolean isEmpty () tests whether this vector does not contain components. E lastelement () returns the last component of this vector. an int lastIndexOf (object elem) returns the index of the last occurrence of the specified object in this vector. int LastIndexOf (object elem, int index) searches backward for the specified object, starts the search at the specified index, and returns an index. E Remove (int index) removes the element at the specified position in this vector. A Boolean remove (Object O) removes the first occurrence of the specified element in this vector, and if the vector does not contain the element, the element remains unchanged. Boolean RemoveAll (collection<?> c) removes all elements contained in the specified Collection from this vector. void Removeallelements () Removes all components from this vector and sets their size to zero. A Boolean removeelement (Object obj) removes the first (least indexed) Match of a variable from this vector. void Removeelementat (int index) deletes the component at the specified index. protected void RemoveRange (int fromIndex, int toindex) removes all elements whose indexes are located between FromIndex (including) and Toindex (not included) from this list. The Boolean retainall (collection<?> C) retains only the elements contained in the specified Collection in this vector. e Set (int index, E Element) replaces the element at the specified position in this vector with the specified element. void Setelementat (E obj, int index) assigns this vector to the group at indexSet to the specified object. void setSize (int newSize) Sets the size of this vector. int size () returns the number of components in this vector. list<e> sublist (int fromIndex, int toindex) returns a partial view of this List, with elements ranging from fromIndex (including) to Toindex (not included). Object[] ToArray () returns an array containing all the elements in the vector that are stored in the correct order. <T> t[] ToArray (t[] a) returns an array that contains all of the elements in the vector in the correct order; The run-time type of the returned array is the type of the specified array. String toString () returns the string representation of this vector, which contains a string representation of each element. void TrimToSize () fine-tunes the capacity of this vector so that it is equal to the current size of the vector.

Detailed use of vectors in java.util.vector

Related Article

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.