How to convert Vector to array in Java
Given a Vector in Java, the task is to convert this Vector to Array.
Examples:
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:
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
Below is the implementation of the above approach:
// Java program to convert // Vector to Array import java.util.*; public class GFG { // Function to convert Vector to Array public static <T> Object[] convertVectorToArray(Vector<T> vector) { // Converting Vector to Array Object[] array = vector.toArray(); return array; } public static void main(String args[]) { // Creating linked list Vector<String> vector = new Vector<String>(); // Adding elements to the linked list vector.add("G"); vector.add("e"); vector.add("e"); vector.add("k"); vector.add("s"); // Print the Vector System.out.println("Vector: " + vector); // Converting Vector to Object Array Object[] objArray = convertVectorToArray(vector); // Convert Object[] to String[] String[] array = Arrays.copyOf(objArray, objArray.length, String[].class); // Print the String Array System.out.println("Array: "+ Arrays.toString(array)); } } |
chevron_right
filter_none
Output:
Vector: [G, e, e, k, s] Array: [G, e, e, k, s]
Method 2:
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()]).
Below is the implementation of the above approach:
// Java program to convert // Vector to Array import java.util.*; public class GFG { public static void main(String args[]) { // Creating linked list Vector<String> vector = new Vector<String>(); // Adding elements to the linked list vector.add("G"); vector.add("e"); vector.add("e"); vector.add("k"); vector.add("s"); // Print the Vector System.out.println("Vector: "+ vector); //Converting Vector to String Array String[] array = vector.toArray(new String[vector.size()]); // Print the String Array System.out.println("Array: "+ Arrays.toString(array)); } } |
chevron_right
filter_none
Output:
Vector: [G, e, e, k, s] Array: [G, e, e, k, s]
Recommended Posts:
- Program to Convert a Vector to List in Java
- How to convert LinkedList to Array in Java?
- Program to convert Array to Set in Java
- Convert byte[] array to File using Java
- Program to Convert Stream to an Array in Java
- Program to convert Array to List in Java
- Program to convert Primitive Array to Stream in Java
- Program to convert Boxed Array to Stream in Java
- Program to convert Byte Array to Writer in Java
- Java Program to convert Character Array to IntStream
- Check if it is possible to reach vector B by rotating vector A and adding vector C to it
- Convert an ArrayList of String to a String array in Java
- Program to Convert Set of Integer to Array of Integer in Java
- Convert Set of String to Array of String in Java
- Java.util.Vector Class in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Rajput-Ji



