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

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

王林
Release: 2023-07-24 19:52:53
Original
1576 people have browsed it

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的向量
Copy after login

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)
Copy after login

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);
        }
    }
}
Copy after login

Running the above code will output the following content:

元素1
元素2
元素3
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template