How to Insert All Elements of Other Collection to Specified Index of Vector in Java


In this example we will show how to insert all elements of other Collection object to specified index of java Vector object with addAll method.

Source Code

package com.beginner.examples;

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

public class InsertAllElementsOfOtherCollectionToVector {

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

	}

}

Output:

After inserting , 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