Copy Elements from Java Vector to Java ArrayList

Problem Statement:

How to Copy Elements from Java Vector to Java ArrayList?

 Copy Elements from Java Vector to Java ArrayList

Description:

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

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

Step3:  Use  Collections.copy(studentList, studentVector);

  • Copies all of the student names elements from one studentVector into student ArrayList.
  • After the copy operation, the index of each copied element (studentVector) in the destination  list (studentList) 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 the source list studentVector.
  • The destination list which in this example is studentList must be at least as long as the source list studentVector.
  • If the destination list studentList is longer, the remaining elements in the destination list studentList are unaffected.
  • Copy method runs in linear time.

Copy Elements from Java Vector to Java ArrayList

Sample Code to Copy Elements from Java Vector to Java ArrayList

package com.stixs.java.collections;

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

/**
 * The Class STXCopyVectorToJavaArrayList will demonstrate how to copy Vector
 * elements - Names of students into an Java ArrayList. This example uses copy()
 * of Collection class - which Copy Elements of Vector to Java ArrayList
 * Example.
 */
public class STXCopyVectorToJavaArrayList {

 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> studentVector = new Vector<String>();

 // Add String elements to Vector
 studentVector.add("John");
 studentVector.add("Eric");
 studentVector.add("Rob");

 // Constructs an empty list with an initial capacity of ten.
 ArrayList<String> studentList = new ArrayList<String>();

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

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

 /*
 * Copies all of the student names elements from one studentVector into
 * studentList.
 * 
 * After the copy operation, the index of each copied element
 * (studentVector) in the destination list (studentList) 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 the
 * source list.
 * 
 * The destination list which in this example is studentList must be at
 * least as long as the source list studentVector.
 * 
 * If the destination list studentVector is longer, the remaining
 * elements in the destination liststudentVector are unaffected.
 * 
 * Copy method runs in linear time.
 */

// Copy from Vector to ArrayList
 Collections.copy(studentList, studentVector);

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

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

seperator

Output:

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

seperator

Java API Classes and Methods used to Copy Elements from Java Vector to Java ArrayList

Collections.copy()

Copy Elements from Java Vector to Java ArrayList

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 Java Vector to Java ArrayList

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.

970 total views, 1 views today

Add comment