Copy Elements from one Java Vector to another Java Vector

Problem Statement:

How to Copy Elements from one Java Vector to another Java Vector?

 Copy Elements from one Java Vector to another Java Vector

Description:

Step1:  Constructs an empty list with an initial capacity of ten.

Step2: Constructs an empty vector so that its internal data array has size

Step3:  Use  Collections.copy(studentVector1, studentVector2);

  • Copies all of the student names elements from one student vector studentVector2 into studentVector1.
  • After the copy operation, the index of each copied element (studentVector2) in the destination  list (studentVector1) will be identical to its index in the source list. Basically it overwrites the elements in the destination list studentVector1 which has the same index in the source list studentVector2.
  • The destination list which in this example is studentVector1 must be at least as long as the source list studentVector2.
  • If the destination list studentVector is longer, the remaining elements in the destination liststudentVector are unaffected.
  • Copy method runs in linear time.

seperator

Sample Code to Copy Elements from one Java Vector to another Java Vector

package com.stixs.java.collections;

import java.util.Collections;
import java.util.Vector;

public class STXCopyVectorElementsToAnotherVector {

 /**
 * The Class STXCopyVectorElementsToAnotherVector will demonstrate how to
 * copy one Vector elements - Names of students into another Java Vector.
 * This example uses copy() of Collection class - which Copy Elements of
 * Vector to Java Vector Example.
 */

 public static void main(String[] args) {

 // Constructs an empty vector so that its internal data array has size
 // 10 and its standard capacity increment is zero.
 Vector<String> studentVector1 = new Vector<String>();

 // Add String elements to Arraylist
 studentVector1.add("John");
 studentVector1.add("Eric");
 studentVector1.add("Rob");

 // Constructs an empty vector so that its internal data array has size
 // 10 and its standard capacity increment is zero.
 Vector<String> studentVector2 = new Vector<String>();

 // Add elements to Vector
 studentVector2.add("Allan");
 studentVector2.add("Black");
 studentVector2.add("Den");
 studentVector2.add("Eian");
 studentVector2.add("Porta");
 studentVector2.add("Anuva");

 System.out.println("STXCopyVectorElementsToAnotherVector:: Student names in the "
 + "studentVector2 before copy method is invoked : " + studentVector2);

 /*
 * Copies all of the student names elements from one student array list
 * into studentVector.
 * 
 * After the copy operation, the index of each copied element
 * (studentList) in the destination list (studentVector) will be
 * identical to its index in the source list. Basically it overwrites
 * the elements in the destination list which has the same index in teh
 * source list.
 * 
 * The destination list which in this example is studentVector must be
 * at least as long as the source list studentList.
 * 
 * If the destination list studentVector is longer, the remaining
 * elements in the destination liststudentVector are unaffected.
 * 
 * Copy method runs in linear time.
 */

 Collections.copy(studentVector2, studentVector1);

 /*
 * Note that If studentVector is not long enough to hold all elements of
 * studentList, the copy method throws IndexOutOfBoundsException.
 */

 System.out.println("STXCopyVectorElementsToAnotherVector:: Student names in the "
 + "studentVector2 after copy method is invoked : " + studentVector2);
 }
}

Copy Elements from one Java Vector to another Java Vector

Output:

STXCopyVectorElementsToAnotherVector:: Student names in the studentVector2 before copy 
method is invoked : [Allan, Black, Den, Eian, Porta, Anuva]
STXCopyVectorElementsToAnotherVector:: Student names in the studentVector2 after copy 
method is invoked : [John, Eric, Rob, Eian, Porta, Anuva]

Copy Elements from one Java Vector to another Java Vector

Java API Classes and Methods used to Copy Elements from one Java Vector to another Java Vector

Collections.copy()

Copy Elements from one Java Vector to another Java Vector

seperator

Reference

Refer below path to get the complete details about Java Collections class.

https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html

seperator

You may also like this

seperator

About TechnoStixs.com

Copy Elements from one Java Vector to another Java Vector

TechnoStixs.com is a dedicated weblog for Java/J2EE and web developers. We take pride in our work. Every publication is carefully analyzed, written and tested to ease the understanding of the topic or subject.

We cover Java Core Technologies, eCommerce, J2EE Frameworks, Web Service, Build Tools, Unit Test Frameworks and Others.

1,290 total views, 3 views today

Add comment