Use of the Vector class

Source: Internet
Author: User

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

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.