Skip to content
Related Articles

Related Articles

Java Program to Convert Array To Vector
  • Last Updated : 07 Jan, 2021

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.

  1. Using Collections.addAll method
  2. Using Arrays.asList() method
  3. 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);
    }
}
Output
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);
    }
}
Output
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);
    }
}
Output
The vector is
[I, love, geeks, for, geeks]

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up