Home  >  Article  >  Java  >  Add elements to vector in Java using addElement() method of Vector class

Add elements to vector in Java using addElement() method of Vector class

王林
王林Original
2023-07-24 19:52:531494browse

Use the addElement() method of the Vector class to add elements to a vector in Java

The Vector class is a data structure in the Java collection framework that can be dynamically resized and can store any type Object. Vectors use the addElement() method to add elements. This article will introduce how to use this method to add elements to vectors.

First, we need to create a Vector object to store elements. You can use the no-argument constructor to create an empty vector, or you can use the constructor with an initial capacity parameter to create a vector with a specified initial capacity. For example:

Vector<String> vector = new Vector<String>();  // 创建一个空的向量
Vector<Integer> vector2 = new Vector<Integer>(10);  // 创建一个初始容量为10的向量

After creating the vector object, we can use the addElement() method to add elements to the vector. The syntax of the addElement() method is as follows:

public void addElement(E obj)

Among them, E is the type of element, and obj is the element to be added. The following is a sample code that demonstrates how to use the addElement() method to add elements to a vector:

import java.util.Vector;
 
public class VectorExample {
    public static void main(String[] args) {
        Vector<String> vector = new Vector<String>();  // 创建一个空的向量
 
        // 添加元素到向量中
        vector.addElement("元素1");
        vector.addElement("元素2");
        vector.addElement("元素3");
 
        // 打印向量的元素
        for (String element : vector) {
            System.out.println(element);
        }
    }
}

Running the above code will output the following content:

元素1
元素2
元素3

As you can see, we use addElement The () method adds three elements to the vector, and traverses the elements of the vector through a for-each loop and prints them out.

It should be noted that although the addElement() method of Vector can dynamically adjust the capacity, if we know the approximate capacity of the vector in advance, it is recommended to specify an appropriate initial capacity when creating the vector, which can improve the vector performance.

To summarize, you can easily add elements to a vector using the addElement() method of the Vector class. Through the above code examples, we can learn to use this method in Java and learn some basic knowledge about using the Vector class. I hope this article can be helpful to readers!

The above is the detailed content of Add elements to vector in Java using addElement() method of Vector class. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn