How to Convert Vector to Array in Java?
Last Updated :
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 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 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:
- 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 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]
Similar Reads
Convert Vector to ArrayList in Java There are multiple ways to convert vector to ArrayList, using passing the Vector in ArrayList constructor and by using simple vector traversal and adding values to ArrayList. Approach 1: Create a Vector.Add some values in Vector.Create a new ArrayList.Traverse vector from the left side to the right
3 min read
How to convert LinkedList to Array in Java? Given a Linked List in Java, the task is to convert this LinkedList to Array. Examples: Input: LinkedList: ['G', 'e', 'e', 'k', 's'] Output: Array: ['G', 'e', 'e', 'k', 's'] Input: LinkedList: [1, 2, 3, 4, 5] Output: Array: [1, 2, 3, 4, 5] Approach: Get the LinkedListConvert the LinkedList to Object
2 min read
Convert List to Array in Java The List interface provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects where duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. Now here we are give
5 min read
How to Convert HashMap to ArrayList in Java? In Java a HashMap is a collection that stores key-value pairs on the other hand, an ArrayList is a collection that stores dynamic arrays. There are some scenarios where we need to convert a HashMap into an ArrayList such as:Extracting only the keys or values in the list form.Converting key-value pai
2 min read
Vector forEach() method in Java The forEach() method of Vector is used to perform a given action for every element of the Iterable of Vector until all elements have been Processed by the method or an exception occurs. The operations are performed in the order of iteration if the order is specified by the method. Exceptions thrown
3 min read