Vector to Array - Java

In this tutorial we will convert vector to Array in java  and to convert vector to array we used method
Vector.toArray();


Syntax:  public Object [] toArray();

See Example:


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

public class Example {

public static void main(String args[]) {
//console method 
vectorToArray();

}
//declare method to get Vector 
public static void vectorToArray(){
//Declare String[] 
String elements[] = { "Book1", "Book2", "Book3", "Book4", "Book5" };
// convert String[] to List
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);
}

}

//result :

Book5
Book3
Book4
Book1
Book2
[Book5, Book3, Book4, Book1, Book2]




EmoticonEmoticon