/* Copy Elements of ArrayList to Java Vector Example, copy elements of arraylist to vector */
import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;
public class CopyArrayListToVectorExample
import java.util.Collections;
import java.util.Vector;
public class CopyArrayListToVectorExample
{
public static void main(String[] args)
public static void main(String[] args)
{
//create an ArrayList object
java.util.ArrayList arrayList = new java.util.ArrayList();
//Add elements to Arraylist
arrayList.add("1");
arrayList.add("4");
arrayList.add("2");
arrayList.add("5");
arrayList.add("3");
//create a Vector object
java.util.Vector v = new java.util.Vector();
//Add elements to Vector
v.add("A");
v.add("B");
v.add("D");
v.add("E");
v.add("F");
v.add("G");
v.add("H");
/*
To copy elements of Java ArrayList to Java Vector use,
static void copy(java.util.List dstList, java.util.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 remains unaffected.
*/
System.out.println("Before copy, Vector Contains : " + v);
//copy all elements of ArrayList to Vector
java.util.Collections.copy(v,arrayList);
System.out.println("After Copy, Vector Contains : " + v);
}
}
Output:
Before copy Vector Contains : [A, B, D, E, F, G, H]
After Copy Vector Contains : [1, 4, 2, 5, 3, G, H]
//create an ArrayList object
java.util.ArrayList arrayList = new java.util.ArrayList();
//Add elements to Arraylist
arrayList.add("1");
arrayList.add("4");
arrayList.add("2");
arrayList.add("5");
arrayList.add("3");
//create a Vector object
java.util.Vector v = new java.util.Vector();
//Add elements to Vector
v.add("A");
v.add("B");
v.add("D");
v.add("E");
v.add("F");
v.add("G");
v.add("H");
/*
To copy elements of Java ArrayList to Java Vector use,
static void copy(java.util.List dstList, java.util.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 remains unaffected.
*/
System.out.println("Before copy, Vector Contains : " + v);
//copy all elements of ArrayList to Vector
java.util.Collections.copy(v,arrayList);
System.out.println("After Copy, Vector Contains : " + v);
}
}
Output:
Before copy Vector Contains : [A, B, D, E, F, G, H]
After Copy Vector Contains : [1, 4, 2, 5, 3, G, H]
2 comments:
well explain and must visit all java collection programs here http://www.javaproficiency.com/2015/05/java-collections-framework-tutorials.html
Thanks for sharing nice article .There are good
Java Collection Tutorials with example
Post a Comment