Vector 在 Java 中是否可以包含异构对象?
由于矢量以对象的形式存储元素,因此你可以在其中存储各种类型(异构)的对象。
示例
import java.util.*; class Demo{} public class VectorSample { public static void main(String args[]) { Demo obj = new Demo(); Vector v = new Vector(3, 2); System.out.println("Initial size: " + v.size()); System.out.println("Initial capacity: " + v.capacity()); v.addElement(new Integer(1)); v.addElement(new String("krishna")); v.addElement(new Float(3.5f)); v.addElement(obj); System.out.println("Capacity after four additions: " + v.capacity()); } }
广告