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

Java 8Object Oriented ProgrammingProgramming

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);
raja
Published on 15-Feb-2019 11:11:32
Advertisements