How to use the asList() method of the Arrays class to create a Vector object


A Vector can be created from an Array using the java.util.Arrays.asList() method.

A program that demonstrates this is given as follows:

Example

 Live Demo

import java.util.Arrays;
import java.util.Vector;

public class Demo {
   public static void main(String args[]) {
      Integer[] arr = { 3, 1, 9, 6, 4, 8, 7 };
      Vector vec = new Vector(Arrays.asList(arr));
      System.out.println("The Vector elements are: " + vec);
   }
}

Output

The Vector elements are: [3, 1, 9, 6, 4, 8, 7]

Now let us understand the above program.

The Integer Array arr[] is defined. Then a Vector is created using the Arrays.asList() method. Then the Vector elements are displayed. A code snippet which demonstrates this is as follows:

Integer[] arr = {3, 1, 9, 6, 4, 8, 7};
Vector<Integer> vec = new Vector(Arrays.asList(arr));
System.out.println("The Vector elements are: " + vec);

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements