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 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() 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()]).
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]
Â
