Easy learning with example program codes

how to copy or clone a vector in java?

The java.util.Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created. It extends AbstractList and implements List interfaces.

The clone() method is used to create similar copy of vector in java.


package com.codesjava;
import java.util.Vector;
public class Test {
  public static void main(String args[]){
	Vector<String> vector = new Vector<String>();
        //adding elements to the end
	System.out.println("Actual vector:"+vector);
        Vector<String> copy = (Vector<String>) vector.clone();
        System.out.println("Cloned vector:"+copy);


Actual vector:[Jai, Mahesh, Hemant, Vishal]
Cloned vector:[Jai, Mahesh, Hemant, Vishal]
Sign Up/ Sign In
Ask a Question

Copyright © 2018 CodesJava Protection Status SiteMap Reference: Java Wiki