Copy Elements from Java ArrayList to Java Vector Example

Problem Statement:

How to Copy Elements from Java ArrayList to Java Vector ?

 Copy Elements from Java ArrayList to Java Vector Example

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(studentVector, studentList);

  • 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 the 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.

seperator

Sample Code to Copy Elements from Java ArrayList to Java Vector

package com.stixs.java.collections;

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

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

 public static void main(String[] args) {

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

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

 // 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 elements to Vector
 studentVector.add("Allan");
 studentVector.add("Black");
 studentVector.add("Den");
 studentVector.add("Eian");
 studentVector.add("Porta");
 studentVector.add("Anuva");


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

 /*
 * 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 the
 * 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(studentVector, studentList);

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

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

seperator

Output:

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

seperator

Java API Classes and Methods used to construct file path

Collections.copy()

Collection Copy

Copy Elements from Java ArrayList to Java Vector

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 ArrayList to 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,265 total views, 1 views today

Add comment