Java Vector copyInto(Object[] anArray) Example

The copyInto(Object[] anArray) method of java.util.Vector class will copy the elements of the vector the specified Array.

Signature

public void copyInto(Object[] anArray)

This method copies the element of the vector into the specified array. The item at index k in this vector is copied into component k of anArray.

Example

The following example shows the usage of java.util.Vector.copyInto(anArray) method.

import java.util.Vector;

public class CopyIntoMethodVectorExample {
    public static void main(String args[])
    {
        // create an empty Vector
        Vector vector1 = new Vector();
        
        // use add() method to add elements to the Vector
        vector1.add("Element1");
        vector1.add("Element2");
        vector1.add("Element3");
        vector1.add("Element4");
        
        //Create an new Array with intial size 4
        String tempArray[] = new String[4];
        
        //Copy Vector element to Array
        vector1.copyInto(tempArray);
        
        System.out.println("**Elements of the Array**");
        //Printing elements of the Array
        for(String temp : tempArray)
        {
            System.out.println(temp);
        }
    }
}

Output

**Elements of the Array**
Element1
Element2
Element3
Element4

Leave a Reply

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