

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert a Vector to an array in Java
A Vector can be converted into an Array using the java.util.Vector.toArray() method. This method requires no parameters and it returns an Array that contains all the elements of the Vector in the correct order.
A program that demonstrates this is given as follows −
Example
import java.util.Vector; public class Demo { public static void main(String args[]) { Vector vec = new Vector(); vec.add(7); vec.add(3); vec.add(5); vec.add(2); vec.add(8); Object[] arr = vec.toArray(); System.out.println("The Array elements are: "); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } }
The output of the above program is as follows −
The Array elements are: 7 3 5 2 8
Now let us understand the above program.
The Vector is created. Then Vector.add() is used to add the elements to the Vector. The method Vector.toArray() is used to convert the Vector into an Array. Then the Array elements are displayed using a for loop. A code snippet which demonstrates this is as follows −
Vector vec = new Vector(); vec.add(7); vec.add(3); vec.add(5); vec.add(2); vec.add(8); Object[] arr = vec.toArray(); System.out.println("The Array elements are: "); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); }
- Related Questions & Answers
- how to convert vector to string array in java
- How to convert a string vector into an integer vector in R?
- Program to convert a Vector to List in Java
- Convert LinkedList to an array in Java
- Java program to convert a list to an array
- Java program to convert an array to a list
- Java program to convert a Set to an array
- Convert an array to reduced form (Using vector of pairs) in C++
- How to convert an array to a list in Java?
- How to convert an Array to a Set in Java?
- Convert elements in a HashSet to an array in Java
- How to convert an object array to an integer array in Java?
- Java Program to convert a set into an Array
- Can we convert a list to an Array in Java?
- How to Convert a Java 8 Stream to an Array?
Advertisements