原文: https://beginnersbook.com/2013/12/copy-all-the-elements-of-one-vector-to-another-vector-example/
在这个例子中,我们将看到如何将Vector的所有元素复制到另一个Vector。该过程用第一向量的对应元素替换第二向量的现有元素。对于例如如果我们将向量v1复制到向量v2,那么v2的第一个元素将被v1的第一个元素替换,依此类推。
在下面的代码中,我们有两个向量va和vb和我们正在使用Collections.copy()(java.util.List, java.util.List))方法将va的所有元素复制到vb。
例:
import java.util.Collections;import java.util.Vector;public class VectorCopyExample {public static void main(String args[]){//First Vector of String typeVector<String> va = new Vector<String>();//Adding elements to the first Vectorva.add("AB");va.add("BC");va.add("CD");va.add("DE");//Second VectorVector<String> vb = new Vector<String>();//Adding elements to the second Vectorvb.add("1st");vb.add("2nd");vb.add("3rd");vb.add("4th");vb.add("5th");vb.add("6th");/*Displaying the elements of second vector beforeperforming the copy operation*/System.out.println("Vector vb before copy: "+vb);//Copying all the elements of Vector va to Vector vbCollections.copy(vb, va);//Displaying elements after copySystem.out.println("Vector vb after copy: "+vb);}}
输出:
Vector vb before copy: [1st, 2nd, 3rd, 4th, 5th, 6th]Vector vb after copy: [AB, BC, CD, DE, 5th, 6th]
