This example shows how to convert a Vector object to an Array in Java. This example also shows how to convert Vector to an array using a for loop, toArray method, and Apache Commons library.
How to convert a Vector to an array in Java?
There are several ways using which we can convert a vector object to an array in Java as given below.
1. Using an enhanced for loop
The simplest solution will be to iterate through Vector elements using a for loop and add vector elements to an array one by one.
The below given example shows how to convert a vector of strings to a String array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.Arrays; import java.util.Vector; public class ConvertVectorToArray { public static void main(String[] args) { //vector of String Vector<String> vColors = new Vector<String>(); vColors.add("Red"); vColors.add("Green"); vColors.add("Blue"); //create new array of string having same size as vector String[] strColors = new String[ vColors.size() ]; //iterate vector for(int i = 0; i < vColors.size(); i++){ //add current element to the array strColors[i] = vColors.get(i); } System.out.println("Vector contains: " + vColors); System.out.println("String array contains: " + Arrays.toString(strColors)); } } |
Output
1 2 |
Vector contains: [Red, Green, Blue] String array contains: [Red, Green, Blue] |
2. Using the toArray method
The Vector toArray
method returns an array containing all the elements of this vector object in the correct order.
1 |
public <T> T[] toArray(T[] a) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//vector of String Vector<String> vColors = new Vector<String>(); vColors.add("Red"); vColors.add("Green"); vColors.add("Blue"); //create new empty array of the same size and type String[] strColors = new String[ vColors.size() ]; //pass this array in the toArray method strColors = vColors.toArray(strColors); System.out.println("String array contains: " + Arrays.toString(strColors)); |
Output
1 |
String array contains: [Red, Green, Blue] |
If the array specified in the toArray
method is large enough to hold all the elements of the vector object, the same array is filled with the elements and returned. If the specified array is smaller than the vector size, a new array of the same type is created, filled with the vector elements and returned.
It is recommended to pass the array of the same size to the toArray
method to avoid the performance penalty of creating a new array.
Important Note: If the array is bigger than the vector size, the array element that comes immediately after the vector elements is set to null to mark the end of the vector elements. However, do not rely on this null value because the vector object itself may contain the null elements.
3. Using the Apache Commons library
If you are using the Apache Commons library in your project, you can use the toPrimitive
method of the ArrayUtils class to convert a vector of wrapper objects to an array of equivalent primitive type.
The below given example shows how to convert a Vector of Integer objects to an array of int values.
1 2 3 4 5 6 7 8 9 |
Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(10); vNumbers.add(20); vNumbers.add(30); int[] intArray = ArrayUtils.toPrimitive( vNumbers.toArray( new Integer[ vNumbers.size() ] ) ); System.out.println("int array contains: " + Arrays.toString(intArray)); |
Output
1 |
int array contains: [10, 20, 30] |
This example is a part of the Java Vector Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Vector Documentation