How to Copy Vector Elements to Array in Java


Here we may learn about how to copy all elements of Vector object to an array with copyInTo method in Java.

Source Code

package com.beginner.examples;

import java.util.Vector;

public class CopyElementsOfVectorToArray {

	public static void main(String[] args) {
		
	    //create Vector. 
	    Vector vector = new Vector();
	    
	    vector.add("A");
	    vector.add("B");
	    vector.add("C");
	    vector.add("D");
	    
	    Object[] objectArray = new Object[4];
	    
	    /*
	     * To copy all elements of java vector object into array use copyInTo(Ojbect[] obj) method.
	     */
	    vector.copyInto(objectArray);
	    
	    System.out.println("ObjectArray Contains..");
	    for(int index=0; index < objectArray.length ; index++) {
	    	System.out.println(objectArray[index]);
	    }
	    	
	}

}

Output:

ObjectArray Contains..
A
B
C
D

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments