How to Convert Vector to Array in Java? - GeeksforGeeks

15 Nov, 2021
As we all know an array is a group of liked-typed variables that are referred to by a common name while on the other hand vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in java.util package and implement the List interface which gives a superior advantage of using all the methods of List interface here. Now the problem simply breaks down to how to use these methods defined in order for the conversion of vector to array for which we will be proposing ways as listed below as follows:
Methods:
Using toArray() method of Vector classUsing toArray(new String[vector.size()]) method
Using toArray() method of Vector class
Using toArray(new String[vector.size()]) method
Let us go through an illustration before landing upon methods to get the understanding of approaches real quick.
Illustrations:
Input : Vector: ['G', 'e', 'e', 'k', 's'] 
Output: Array: ['G', 'e', 'e', 'k', 's'] 
Input : Vector: [1, 2, 3, 4, 5]
Output: Array: [1, 2, 3, 4, 5] 
Method 1: Using toArray() method of Vector class
Approach: 
Get the Vector.Convert the Vector to Object array using toArray() methodConvert the Object array to desired type array using Arrays.copyOf() methodReturn the print the Array.
Get the Vector.
Convert the Vector to Object array using toArray() method
Convert the Object array to desired type array using Arrays.copyOf() method
Return the print the Array.
Example:
Java
// Java Program to Convert Vector to Array  // Importing required classesimport java.util.*;  // Main classpublic class GFG {      // Method 1    // To convert Vector to Array    public static <T>        Object[] convertVectorToArray(Vector<T> vector)    {        // Converting vector to array        // using toArray() method of Vector class        Object[] array = vector.toArray();          // Returning the array        return array;    }      // Method 2    // Main driver method    public static void main(String args[])    {        // Creating a vector of string type        Vector<String> vector = new Vector<String>();          // Adding custom elements using add() method        vector.add("G");        vector.add("e");        vector.add("e");        vector.add("k");        vector.add("s");          // Printing the vector elements        System.out.println("Vector: " + vector);          // Converting vector to object array        Object[] objArray = convertVectorToArray(vector);          // Converting object array to string array        String[] array = Arrays.copyOf(            objArray, objArray.length, String[].class);          // Lastly printing the string array        System.out.println("Array: "                           + Arrays.toString(array));    }}
Vector: [G, e, e, k, s]
Array: [G, e, e, k, s]
 
Method 2: Using toArray(new String[vector.size()]) method 
Approach: 
Created a Vector String type.Added elements into Vector using add(E) method.Converted the Vector to Array using toArray(new String[vector.size()]).
Created a Vector String type.
Added elements into Vector using add(E) method.
Converted the Vector to Array using toArray(new String[vector.size()]).
Example:
Java
// Java Program to Convert Vector to Array  // Importing required classesimport java.util.*;  // Main classpublic class GFG {      // Main driver method    public static void main(String args[])    {        // Creating a vector of string type        Vector<String> vector = new Vector<String>();          // Adding elements to above object        // using add() method        vector.add("G");        vector.add("e");        vector.add("e");        vector.add("k");        vector.add("s");          // Printing the elements inside vector        System.out.println("Vector: " + vector);          // Converting vector to string array        String[] array            = vector.toArray(new String[vector.size()]);          // Printing the string array        System.out.println("Array: "                           + Arrays.toString(array));    }}
Vector: [G, e, e, k, s]
Array: [G, e, e, k, s]
 
Rajput-Ji
solankimayank
Java-Array-Programs
Java-Arrays
Java-Collections
Java-Vector
Technical Scripter 2018
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Initialize an ArrayList in Java
HashMap in Java with Examples
Interfaces in Java
How to iterate any Map in Java
ArrayList in Java
Multidimensional Arrays in Java
Stack Class in Java
Stream In Java
Singleton Class in Java
Set in Java