Vector allAdd() method in Java with an example



In this source code example, we will demonstrate the usage of the Vector addAll() method in Java with an example.

Vector addAll(Collection C) Method

java.util.Vector.addAll(Collection C): This method is used to append all of the elements from the collection passed as a parameter to this function to the end of a vector keeping in mind the order of return by the collection’s iterator.

Vector addAll(Collection C) Method Example

The below program illustrates the working of the Vector addAll(Collection C) method:
// Java code to illustrate boolean addAll() 
import java.util.*; 
import java.util.ArrayList; 

public class VectorAddAllExample { 
	public static void main(String args[]) 
	{ 

		// Creating an empty Vector 
		Vector tenPrimeNumbers  = new Vector(); 

		// Use add() method to add elements in the Vector 
		tenPrimeNumbers.add(2);
		tenPrimeNumbers.add(3);
		tenPrimeNumbers.add(5);
		tenPrimeNumbers.add(7);
		tenPrimeNumbers.add(11);

		List nextFivePrimeNumbers = new ArrayList<>();
		nextFivePrimeNumbers.add(13);
		nextFivePrimeNumbers.add(17);
		nextFivePrimeNumbers.add(19);
		nextFivePrimeNumbers.add(23);
		nextFivePrimeNumbers.add(29);

		// Displaying the Vector 
		System.out.println("The Vector is: " + tenPrimeNumbers); 

		// Appending the collection to the vector 
		tenPrimeNumbers.addAll(nextFivePrimeNumbers); 

		// Clearing the vector using clear() and displaying 
		System.out.println("The new vector is: " + tenPrimeNumbers); 
	} 
} 

    

Output:

The Vector is: [2, 3, 5, 7, 11]
The new vector is: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]         

Vector ajava.util.Vector.addAll(int index, Collection C) Method

java.util.Vector.addAll(int index, Collection C): This method is used to append all of the elements from the collection passed as a parameter to this function at a specific index or position of a vector..

java.util.Vector.addAll(int index, Collection C) Method Example

The below program illustrates the working of the Vector addAll(Collection C) method:
import java.util.ArrayList;
import java.util.Vector;

/*
 *  Example of addAll(int index, Collection c) method 
 */
public class VectorExample
{

	public static void main( String[] args )
	{
		Vector vector = new Vector<>();

		vector.add(10);
		vector.add(20);
		vector.add(30);

		System.out.println("vector  : " + vector + "\n");

		ArrayList arrayList = new ArrayList();
		arrayList.add(400);
		arrayList.add(500);

		System.out.println("arrayList  : " + arrayList + "\n");

		/*
		 * Inserts all of the elements in the specified Collection into this
		 * Vector at the specified position. Shifts the element currently at
		 * that position (if any) and any subsequent elements to the right
		 * (increases their indices). The new elements will appear in the Vector
		 * in the order that they are returned by the specified Collection's
		 * iterator.
		 */
		boolean isAdded = vector.addAll(2, arrayList);

		System.out.println("isAdded : " + isAdded);
		System.out.println("vector  : " + vector + "\n");

	}
}  

Output:

vector  : [10, 20, 30]

arrayList  : [400, 500]

isAdded : true
vector  : [10, 20, 400, 500, 30]

References

Comments