📜  Java中的向量addElement()方法

📅  最后修改于: 2022-05-13 01:54:52.298000             🧑  作者: Mango

Java中的向量addElement()方法

Java.util.Vector .addElement()方法用于通过将向量的大小增加 1 来将指定元素附加到该向量的末尾。该方法的功能类似于向量类。
句法:

boolean addElement(Object element)

参数:此函数接受对象类型的单个参数元素,并引用此参数指定的元素附加到向量的末尾。
返回值:这是一个 void 类型的方法,不返回任何值。
下面的程序说明了Java.util.Vector.add(Object element) 方法的工作。
程序 1:

Java
// Java code to illustrate boolean addElement()
import java.util.*;
 
public class GFG {
    public static void main(String args[])
    {
 
        // Creating an empty Vector
        Vector vec_tor = new Vector();
 
        // Use add() method to add elements in the vector
        vec_tor.add("Geeks");
        vec_tor.add("for");
        vec_tor.add("Geeks");
        vec_tor.add("10");
        vec_tor.add("20");
 
        // Output the present vector
        System.out.println("The vector is: " + vec_tor);
 
        // Adding new elements to the end
        vec_tor.addElement("Last");
        vec_tor.addElement("Element");
 
        // Printing the new vector
        System.out.println("The new Vector is: " + vec_tor);
    }
}


Java
// Java code to illustrate boolean addElement()
import java.util.*;
 
public class VectorDemo {
    public static void main(String args[])
    {
 
        // Creating an empty Vector
        Vector vec_tor = new Vector();
 
        // Use add() method to add elements in the vector
        vec_tor.add(1);
        vec_tor.add(2);
        vec_tor.add(3);
        vec_tor.add(10);
        vec_tor.add(20);
 
        // Output the present vector
        System.out.println("The vector is: " + vec_tor);
 
        // Adding new elements to the end
        vec_tor.addElement(50);
        vec_tor.addElement(100);
 
        // Printing the new vector
        System.out.println("The new Vector is: " + vec_tor);
    }
}


输出:
The vector is: [Geeks, for, Geeks, 10, 20]
The new Vector is: [Geeks, for, Geeks, 10, 20, Last, Element]

方案二:

Java

// Java code to illustrate boolean addElement()
import java.util.*;
 
public class VectorDemo {
    public static void main(String args[])
    {
 
        // Creating an empty Vector
        Vector vec_tor = new Vector();
 
        // Use add() method to add elements in the vector
        vec_tor.add(1);
        vec_tor.add(2);
        vec_tor.add(3);
        vec_tor.add(10);
        vec_tor.add(20);
 
        // Output the present vector
        System.out.println("The vector is: " + vec_tor);
 
        // Adding new elements to the end
        vec_tor.addElement(50);
        vec_tor.addElement(100);
 
        // Printing the new vector
        System.out.println("The new Vector is: " + vec_tor);
    }
}
输出:
The vector is: [1, 2, 3, 10, 20]
The new Vector is: [1, 2, 3, 10, 20, 50, 100]