How to Append All ArrayList Elements to Java Vector


This example would help us learn how to append all elements of ArrayList object at the end of Vector object with addAll method in Java.

Source Code

package com.beginner.examples;

import java.util.ArrayList;
import java.util.Vector;

public class AppendAllElementsOfOtherCollectionToVector {

	public static void main(String[] args) {
		
	    //create Vector. 
	    Vector vector = new Vector();
	    
	    vector.add("A");
	    vector.add("B");
	    vector.add("C");
	    vector.add("D");
	    
	    //create a new ArrayList object.
	    ArrayList arrayList = new ArrayList();
	    arrayList.add("1");
	    arrayList.add("2");
	    
	    /*
	     * To append all elements of another Collection to Vector use addAll(Collection c) method.
	     */
	    vector.addAll(arrayList);
	    System.out.println("After append elements of ArrayList, vector contains :");
	    for(int index=0; index < vector.size() ; index++) {
	    	System.out.println(vector.get(index));
	    }

	}

}

Output:

After append elements of ArrayList, vector contains :
A
B
C
D
1
2

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments