1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
/* Copy Elements of One Java Vector to Another Java Vector Example This java example shows how to copy all elements of one Java Vector object to another Java Vector object using copy method of Collections class. */ import java.util.Collections; import java.util.Vector; public class CopyElementsOfVectorToVectorExample { public static void main(String[] args) { //create first Vector object Vector v1 = new Vector(); //Add elements to Vector v1.add("1"); v1.add("2"); v1.add("3"); //create another Vector object Vector v2 = new Vector(); //Add elements to Vector v2.add("One"); v2.add("Two"); v2.add("Three"); v2.add("Four"); v2.add("Five"); /* To copy elements of one Java Vector to another use, static void copy(List dstList, List sourceList) method of Collections class. This method copies all elements of source list to destination list. After copy index of the elements in both source and destination lists would be identical. The destination list must be long enough to hold all copied elements. If it is longer than that, the rest of the destination list's elments would remain unaffected. */ System.out.println("Before copy, Second Vector Contains : " + v2); //copy all elements of Vector to another Vector using copy //method of Collections class Collections.copy(v2,v1); /* Please note that, If destination Vector object is not long enough to hold all elements of source Vector, it throws IndexOutOfBoundsException. */ System.out.println("After copy, Second Vector Contains : " + v2); } } /* Output would be Before copy, Second Vector Contains : [One, Two, Three, Four, Five] After copy, Second Vector Contains : [1, 2, 3, Four, Five] */ |
Add Comment