Saturday 10 May 2014

Vector : toArray() : get the Vector Data as object Array

public synchronized Object[] toArray()
Returns an array containing all of the elements in this Vector in the correct order.

import java.util.*;

class VectorToArray{
 public static void main(String args[]){
  Vector<Integer> myVector;
  Object arr[];
  
  myVector = new Vector<> ();
  
  /* Add Elements to myVector */
  myVector.add(10);
  myVector.add(20);
  myVector.add(30);
  myVector.add(40);
  
  int size = myVector.size();
  arr = myVector.toArray();
  
  System.out.println("Elements in myVector are");
  System.out.println(myVector);
  
  System.out.println("Elements in arr are");
  for(Object i: arr)
   System.out.print(i +" ");
 }
}

Output
Elements in myVector are
[10, 20, 30, 40]
Elements in arr are
10 20 30 40



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment