Can a Vector contain heterogeneous objects in Java?


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


Updated on: 30-Jul-2019

762 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements