java.util.Vector.toArray() Method


Advertisements


Description

The toArray() method is used to return an array containing all of the elements in this Vector in the correct order.

Declaration

Following is the declaration for java.util.Vector.toArray() method

public Object[] toArray()

Parameters

NA

Return Value

The method call returns an array containing all of the elements in this collection.

Exception

NA

Example

The following example shows the usage of java.util.Vector.toArray() method.

Live Demo
package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import java.util.Vector;

public class VectorDemo {
   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);
   }
}

Let us compile and run the above program, this will produce the following result.

Q
P
M
N
O
[Q, P, M, N, O]]

java_util_vector.htm

Advertisements