Java Utility Library

Java Vector - toArray() Method



The java.util.Vector.toArray() method returns an array containing all of the elements in the vector in the correct order.

Syntax

public Object[] toArray()

Parameters

No parameter is required.

Return Value

Returns an array containing all elements of the vector.

Exception

NA

Example:

In the example below, the java.util.Vector.toArray() method returns an array containing all elements of the given vector.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a vector
    Vector<Integer> MyVector = new Vector<Integer>();

    //populating vector
    MyVector.add(50);
    MyVector.add(20);
    MyVector.add(30);   

    //convert the vector into an array
    Object[] Arr =  MyVector.toArray();

    //print the array
    System.out.print("The Arr contains: ");
    for(int i = 0; i < Arr.length; i++)
      System.out.print(Arr[i]+ " ");
  }
}

The output of the above code will be:

The Arr contains: 50 20 30 

❮ Java.util - Vector