Copy Elements of ArrayList to Java Vector Example

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

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine

Related Java Examples





Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!