Skip to Content

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. */

Advertisement: