Append all elements of other Collection to Java Vector Example

  1. /*
  2.   Append all elements of other Collection to Java Vector Example
  3.   This Java Example shows how to append all elements of other Collection object
  4.   at the end of Java Vector object using addAll method. This program shows
  5.   how to append all elements of Java ArrayList to Java Vector object.
  6. */
  7.  
  8. import java.util.Vector;
  9. import java.util.ArrayList;
  10.  
  11. public class AppendAllElementsOfOtherCollectionToVectorExample {
  12.  
  13. public static void main(String[] args) {
  14.  
  15. //create Vector object
  16. Vector v = new Vector();
  17.  
  18. //Add elements to Vector
  19. v.add("1");
  20. v.add("2");
  21. v.add("3");
  22.  
  23. //create a new ArrayList object
  24. ArrayList arrayList = new ArrayList();
  25. arrayList.add("4");
  26. arrayList.add("5");
  27.  
  28. /*
  29.   To append all elements of another Collection to Vector use
  30.   boolean addAll(Collection c) method.
  31.   It returns true if the Vector was changed by the method call.
  32.   */
  33.  
  34. //append all elements of ArrayList to Vector
  35. v.addAll(arrayList);
  36.  
  37. //display elements of Vector
  38. System.out.println("After appending all elements of ArrayList,
  39. Vector contains..");
  40. for(int i=0; i<v.size(); i++)
  41. System.out.println(v.get(i));
  42.  
  43. }
  44. }
  45.  
  46. /*
  47. Output would be
  48. After appending all elements of ArrayList, Vector contains..
  49. 1
  50. 2
  51. 3
  52. 4
  53. 5
  54. */

Post new comment

To combat spam, please enter the code in the image.


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!