Can a Vector contain heterogeneous objects in Java?

Java 8Object Oriented ProgrammingProgramming

Since a vector stores elements in the form of objects, you can store objects of various types (heterogeneous) in it.

Example:

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());
   }
}


raja
Published on 17-Jan-2018 05:50:49
Advertisements