Java 中,addElement(E obj) 方法有什么作用?
addElement(E obj) 方法用于将指定的组件添加到此向量的末尾,并将其大小增加 1。如果向量的尺寸大于其容量,则将增大此向量的容量。此 addElement() 方法在功能上与 add(Object) 方法相同。add() 方法返回 true/false,而 addElement() 方法不返回任何值。
示例
import java.util.Vector; public class VectorDemo { public static void main(String[] args) { Vector<Integer> vec = new Vector<Integer>(4); vec.add(4); vec.add(3); vec.add(2); vec.add(1); System.out.println("Initial elements in the vector :- "); for (Integer number : vec) { System.out.println("Number = " + number); } vec.addElement(12); System.out.println("Elements after addition :- "); for (Integer number : vec) { System.out.println("Number = " + number); } } }
输出
Initial elements in the vector :- Number = 4 Number = 3 Number = 2 Number = 1 Elements after addition :- Number = 4 Number = 3 Number = 2 Number = 1 Number = 12
广告