How To Copy Vector To Array

import java.util.Vector;

public class VectorToArray {

public static void main(String[] args) {

Vector<String> vector = new Vector<String>();
//add element in vector
vector.add(“cricket”);
vector.add(“hockey”);
vector.add(“football”);
vector.add(“tennish”);

// print vector
System.out.println(“Vector = “+vector);

String [] arr = new String[vector.size()];

//Returns an array containing all of the
//elements in this Vector in the correct order.

vector.toArray(arr);

System.out.println(“Array is :”);

for (int i = 0; i < arr.length; i++) {
System.out.println(“Element is = “+arr[i]);

}

}

}

Output:

Vector = [cricket, hockey, football, tennish]

Array is :

Element is = cricket

Element is = hockey

Element is = football

Element is = tennish

Leave a Reply

Your email address will not be published. Required fields are marked *

− 1 = 1