打印向量的元素
您可以使用 println 语句直接打印向量的所有元素。
System.out.println(vect);
或者,您可以使用 hasMoreElements() 和 nextElement() 方法逐个打印其元素。
示例
import java.util.*; public class VectorPrintingElements { public static void main(String args[]) { // initial size is 3, increment is 2 Vector v = new Vector(); v.addElement(new Integer(1)); v.addElement(new Integer(2)); v.addElement(new Integer(3)); v.addElement(new Integer(4)); System.out.println("Capacity after four additions: " + v.capacity()); // enumerate the elements in the vector. Enumeration vEnum = v.elements(); System.out.println("\nElements in vector:"); while(vEnum.hasMoreElements()) System.out.print(vEnum.nextElement() + " "); System.out.println(); } }
输出
Capacity after four additions: 10 Elements in vector: 1 2 3 4
广告