Home » Core Java » threads » Vector copy example

About Ilias Tsagklis

Avatar photo
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Vector copy example

With this example we are going to demonstrate how to get a copy of a Vector. We are using the synchronized statement in order to take a safe copy of the Vector’s elements. In short, to get a copy of a Vector you should:

  • Create a new Vector.
  • Populate the vector with elements, using addElement(Object obj) API method of Vector.
  • Set the Vector in a synchronized statement.
  • Create a new String array with the size equal to the Vector.size().
  • Get each one of the Vector’s elements, using elementAt(int index) API method of Vector and put it in the same index of the array.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.util.Vector;

public class VectorCopyExample {

    public static void main(String[] args) {


  Vector vector = new Vector();





  String[] wordArray;

  synchronized (vector) {

int size = vector.size();

wordArray = new String[size];

for (int i = 0; i < wordArray.length; i++) {

    wordArray[i] = (String) vector.elementAt(i);



  System.out.println("word.length" + wordArray.length);

  for (int i = 0; i < wordArray.length; i++) {

System.out.println("[" + i + "]=" + wordArray[i]);




This was an example of how to get a copy of a Vector in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments