Java Vector.toArray()

Syntax

Vector.toArray() has the following syntax.

public Object [] toArray()

Example

In the following code shows how to use Vector.toArray() method.


//from   w  ww . j a  v  a2s.  c o m

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class Main {
   public static void main(String[] args) {
      String elements[] = { "M", "N", "O", "P", "Q" };
      Set set = new HashSet(Arrays.asList(elements));

      String[] strObj = new String[set.size()];

      strObj = (String[]) set.toArray(strObj);

      for (int i = 0; i < strObj.length; i++) {
         System.out.println(strObj[i]);
      }
      System.out.println(set);
   }    
}

The code above generates the following result.