Open In App

How to Convert Vector to Array in Java?

Last Updated : 15 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Using toArray() method of Vector class
  2. 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: 

  1. Get the Vector.
  2. Convert the Vector to Object array using toArray() method
  3. Convert the Object array to desired type array using Arrays.copyOf() method
  4. Return the print the Array.

Example:

Java




// Java Program to Convert Vector to Array
  
// Importing required classes
import java.util.*;
  
// Main class
public 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));
    }
}


Output: 

Vector: [G, e, e, k, s]
Array: [G, e, e, k, s]

 

Method 2: Using toArray(new String[vector.size()]) method 

Approach: 

  1. Created a Vector String type.
  2. Added elements into Vector using add(E) method.
  3. Converted the Vector to Array using toArray(new String[vector.size()]).

Example:

Java




// Java Program to Convert Vector to Array
  
// Importing required classes
import java.util.*;
  
// Main class
public 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));
    }
}


Output: 

Vector: [G, e, e, k, s]
Array: [G, e, e, k, s]

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads