The detailed use of vectors in Java.util.vector

Source: Internet
Author: User
Tags addall

ArrayList will be faster than vector, he is non-synchronous, if the design involves multi-threading, or vector better

Import java.util.*;

/**

* Demonstrates the use of vectors. including the creation of vectors, adding elements to vectors, removing elements from vectors,

* Count the number of elements in the vector and traverse the elements in the vector.

*/

public class vectordemo{

public static void Main (string[] args) {

Creation of vectors

Create using the construction method of the vector

Vector v = new vector (4);

Adding elements to the vector

Adding elements directly using the Add method

V.add ("Test0");

V.add ("Test1");

V.add ("Test0");

V.add ("Test2");

V.add ("Test2");

Remove an element from the vector

V.remove ("Test0");

Delete the element of the specified content

V.remove (0);

Delete an element by index number

Get the number of elements already in the vector

int size = V.size ();

SYSTEM.OUT.PRINTLN ("Size:" + size);

Traversing elements in a 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 ();

}

}

Changes in vectors: 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. 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 the elements in the specified Collection to the end of this vector, appending the elements in the order returned by the iterator for the specified collection.

Boolean addall (int index, COLLECTION<? 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.

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 component of this vector to 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)

Increase 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.

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.

int 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.

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 backwards 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.

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.

Boolean removeelement (Object obj)

Removes the first (least indexed) occurrence 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 between fromIndex (inclusive) and Toindex (not included) from this list.

Boolean retainall (collection<?> c)

Only the elements contained in the specified Collection are preserved 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) sets this vector to the specified object at the component at index. 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.

The detailed use of vectors in Java.util.vector

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.