Array is a group of like-typed variables that are referred to by a common name. Java arrays can be both types namely primitive data types or object (or non-primitive) reference of a class. In the case of primitive data types, the actual values are stored in contiguous memory locations, while in the case of objects of a class, the actual objects are stored in a heap segment.
Vector class implements a growable array of objects. It is compatible with Java collections. Vector implements a dynamic array–means it can grow or shrink as required. It contains components that can be accessed using an integer index like an array.
There are 3 ways to convert an array to a vector in Java.
- Using Collections.addAll method
- Using Arrays.asList() method
- Using loop
Method 1: Using Collections.addAll method
Syntax:
public static boolean addAll(Collection c, T... elements)
Parameters: This method takes the following argument as a parameter
- c- the collection into which elements are to be inserted
- elements- the elements to insert into c
Return Value: This method returns true if the collection changed as a result of the call.
Example: Make an array and an empty vector, pass the filled array and empty vector in the method and the vector will be filled with the array element.
Java
// Java program to Convert Array To Vector // Using Collections.addAll() method import java.util.*; public class array_to_vector { public static void main(String[] args) { String[] arr = { "I" , "love" , "geeks" , "for" , "geeks" }; // create a new vector object of the same type Vector<String> v = new Vector<String>(); // Use the addAll method of the Collections to add // all array elements to the vector object Collections.addAll(v, arr); System.out.println( "The vector is" ); // printing vector System.out.println(v); } } |
The vector is [I, love, geeks, for, geeks]
Method 2: Using Arrays.asList() method
Syntax:
public static List asList(T... a)
Parameters: This method takes the array a which is required to be converted into a List.
Example: The Vector constructor can take a List object and convert it to a vector. So, convert the array to the List and pass it to the vector constructor.
Java
// Java program to Convert Array To Vector // Using Arrays.asList() method import java.util.*; public class array_to_vector { public static void main(String[] args) { String[] arr = { "I" , "love" , "geeks" , "for" , "geeks" }; // create a new vector object of the same type Vector<String> v = new Vector<String>(Arrays.asList(arr)); System.out.println( "The vector is" ); // printing vector System.out.println(v); } } |
The vector is [I, love, geeks, for, geeks]
Method 3: Using loop
Example: Loop through each element of the array and add that element one by one in the vector.
Java
// Java program to Convert Array To Vector // Using simple iteration method import java.util.*; public class array_to_vector { public static void main(String[] args) { String[] arr = { "I" , "love" , "geeks" , "for" , "geeks" }; // create a new vector object of the same type Vector<String> v = new Vector<String>(); for ( int i = 0 ; i < arr.length; i++) v.addElement(arr[i]); System.out.println( "The vector is" ); // printing vector System.out.println(v); } } |
The vector is [I, love, geeks, for, geeks]