Java Collection Tutorial - Java Vector.toArray(T[] a)








Syntax

Vector.toArray(T[] a) has the following syntax.

public <T> T[] toArray(T[] a)

Example

In the following code shows how to use Vector.toArray(T[] a) method.

//from w  ww.j  a v  a 2  s  .c  om

import java.util.*;
import java.util.Arrays;

public class Main {
   public static void main(String[] args) {
      Vector<Integer>  vec = new Vector<Integer> (4);
      Integer[] anArray = new Integer[4];

      vec.add(4);
      vec.add(3);
      vec.add(2);
      vec.add(1);
      
      // fill the array from the vector
      vec.toArray(anArray); 

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

The code above generates the following result.