Java Program to Copy Elements of Vector Collection Into an Array

You are currently viewing Java Program to Copy Elements of Vector Collection Into an Array

Java program to copy elements of Vector collection into an array

// Java program to copy elements of Vector collection 
// into an array

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Vector < String > vec = new Vector < String > ();

    String vehicles[] = new String[5];

    vec.add("CAR");
    vec.add("BUS");
    vec.add("BIKE");
    vec.add("TRUCK");

    System.out.println("Vector elements: " + vec);

    vec.copyInto(vehicles);

    System.out.println("Array elements: ");
    for (String v: vehicles)
      System.out.println(" " + v);
  }
}

Output

Vector elements: [CAR, BUS, BIKE, TRUCK]
Array elements: 
 CAR
 BUS
 BIKE
 TRUCK
 null

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply