Monday, 5 May 2014

public Vector( Collection c)

public Vector(Collection<? extends E> c)
Constructs a vector containing the elements of the specified collection, in the order they are returned by the collection's iterator.

import java.util.*;
class VectorConstructor4{
 public static void main(String args[]){
  Vector<Integer> myVector;
  Collection<Integer> myColl = new ArrayList<> ();
  
  /* Add Elements to myColl */
  myColl.add(10);
  myColl.add(20);
  myColl.add(30);
  
  myVector = new Vector<> (myColl);
  
  System.out.println("Elements in myVector are");
  System.out.println(myVector);
  System.out.println("Elements in myColl are");
  System.out.println(myColl);
 }
}

Output
Elements in myVector are
[10, 20, 30]
Elements in myColl are
[10, 20, 30]

1. throws NullPointerException if the specified collection is null
import java.util.*;
class VectorConstructor4Null{
 public static void main(String args[]){
  Vector<Integer> myVector = new Vector<> (null);
 }
}

Output
Exception in thread "main" java.lang.NullPointerException
        at java.util.Vector.<init>(Vector.java:167)
        at VectorConstructor4Null.main(VectorConstructor4Null.java:4)


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment