Java vector to array: In the previous article, we have seen Java Program to Convert an Array into Collection
In this article we are going to see how to convert vector to array using Java programming language.
Java Program to Convert Vector to Array
Array:
Convert vector to array: Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Vector:
vector.toarray: Vector is a class which implements the List Interface. It is commonly used data structure as it allows to create resizable arrays. Means while arrays are static in nature, vector can grow or shrink its’ size depending on elements. While elements will be added into vector it can grow its’ size and while elements will be removed vector can reduce its size.
Let’s see different ways to convert vector to array.
Method-1: Java Program to Convert Vector to Array By Using toArray() Method and Arrays.copyOf() Method
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.
Program:
import java.util.*; public class Main { public static void main(String args[]) { // Creating vector Vector<String> vec = new Vector<String>(); // Adding elements to the vector vec.add("J"); vec.add("a"); vec.add("v"); vec.add("a"); // Print the Vector System.out.println("Vector: " + vec); // Converting Vector to Object Array Object[] objArray = convertVectorToArray(vec); // Convert Object[] to String[] String[] arr = Arrays.copyOf(objArray, objArray.length, String[].class); // Print the String Array System.out.println("Array: " + Arrays.toString(arr)); } // Function to convert Vector to Array public static <T> Object[] convertVectorToArray(Vector<T> vec) { // Converting Vector to Array Object[] arr = vec.toArray(); return arr; } }
Output: Vector: [J, a, v, a] Array: [J, a, v, a]
Method-2: Java Program to Convert Vector to Array By Using toArray() Method
Approach:
- Created a Vector String type.
- Added elements into Vector using
add(E)
method. - Convert the Vector to Array using
toArray(new String[vector.size()])
.
Program:
import java.util.*; public class Main { public static void main(String args[]) { // Creating vector Vector<String> vec = new Vector<String>(); // Adding elements to the vector Scanner sc = new Scanner(System.in); System.out.print("Enter the number of elements: "); int n = sc.nextInt(); sc.nextLine(); // to consume the newline character System.out.println("Enter the elements: "); for (int i = 0; i < n; i++) { vec.add(sc.next()); } // Print the Vector System.out.println("Vector: " + vec); // Converting Vector to String Array String[] arr = vec.toArray(new String[vec.size()]); // Print the String Array System.out.println("Array: " + Arrays.toString(arr)); } }
Output: Enter the number of elements: 4 Enter the elements: Btech Geeks Best Platform Vector: [Btech, Geeks, Best, Platform] Array: [Btech, Geeks, Best, Platform]
Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.
Related Java Programs:
- Java Program to Convert Linked List to Array
- Java Program to Print the Array Element Address When the Base Address and Array Element Size is Given
- Java Program to sort array alternatively based on highest average of even or odd elements of the array
- Java Program to Replace all the Positive Elements of the Array with 1 and all the Negative Elements with 0