First, the vector class in Java can implement an automatically growing array of objects, in contrast to the use of ArrayList class is faster than the vector class, wherein the ArrayList is not synchronous;
Second, if the design involves multi-threading, it is even more of a vector.
1. Code examples using the vector class:
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) {
Create using the construction method of the vector
Vector v = new vector (4);
V.add ("Test0");
V.add ("Test1");
V.add ("Test2");
V.add ("Test3");
V.add ("Test2");
V.remove ("Test2"); Deletes the element of the specified content, if there are multiple elements of the same content, deletes only the first
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));
}
}
}
Common methods of 2.Vector:
Public string[] Dropstopwords (string[] oldwords) {
vector<string> v1 = new vector<string> ();//vector has the advantage of being able to grow dynamically
for (int i=0;i<oldwords.length;++i) {
if (Stopwordshandler.isstopword (Oldwords[i]) ==false) {
V1.add (Oldwords[i]);//Not stop word
}
}
string[] Newwords = new string[v1.size ()];
V1.toarray (newwords);//Use vector method ToArray to write whole array of vectors into string array
return newwords;
}
Use of the Vector class