In the previous article we have discussed Java Vector addElement() Method with Examples
In this article you will see the use of Vector addAll() method in java along with suitable examples.
Java Vector addAll() Method with Examples
Let’s see addAll() method with examples to understand it more clearly.
Void addAll(Collection c):
This java.util.Vector.addAll(Collection c)
method adds all the elements inside a collection to the end of a vector at once. This method is of type void hence it does not return anything.
Syntax-
vectorName.addAll(Collection col)
Where,
vectorName
refers to the name of your Vector.Collection col
refers to the collection that will be added into the vector.
Approach:
- Create a vector of type Integer and add elements into it.
- Create a Collection of type Integer and add elements into it.
- Add the collection to vector by using
addAll(Collection col)
method. - Print the new vector.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create a Vector of Integer datatype with size 6 Vector<Integer> vec = new Vector<Integer>(6); // Adding some elements to the vector vec.add(11); vec.add(65); vec.add(82); vec.add(27); vec.add(89); vec.add(46); // Prints the old vector elements and the vector size System.out.println("The old vector is "+vec+" of size "+vec.size()); //collection created Collection<Integer> col = new ArrayList<Integer>(); col.add(29); col.add(38); col.add(74); //Adding a collection to Vector vec.addAll(col); // Prints the vector elements and the vector size System.out.println("The new vector is "+vec+" of size "+vec.size()); } }
Output: The old vector is [11, 65, 82, 27, 89, 46] of size 6 The new vector is [11, 65, 82, 27, 89, 46, 29, 38, 74] of size 9
Void addAll(int Index, Collection c):
This java.util.Vector.addAll(int Index, Collection c)
method adds all the elements inside a collection to the specified index of a vector at once. This method is of type void hence it does not return anything.
Syntax-
vectorName.addAll(int Index, Collection col)
Where,
int Index
refers to the index of vector where Collection will be added.
Approach:
- Create a vector of type Integer and add elements into it.
- Create a Collection of type Integer and add elements into it.
- Add the collection to the specified index of vector by using
addAll(int Index, Collection col)
method. - Print the new vector.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create a Vector of Integer datatype with size 6 Vector<Integer> vec = new Vector<Integer>(6); // Adding some elements to the vector vec.add(11); vec.add(65); vec.add(82); vec.add(27); vec.add(89); vec.add(46); // Prints the old vector elements and the vector size System.out.println("The old vector is "+vec+" of size "+vec.size()); //collection created Collection<Integer> col = new ArrayList<Integer>(); col.add(29); col.add(38); col.add(74); //Adding a collection from index-1 of the Vector vec.addAll(1,col); // Prints the vector elements and the vector size System.out.println("The new vector is "+vec+" of size "+vec.size()); } }
Output: The old vector is [11, 65, 82, 27, 89, 46] of size 6 The new vector is [11, 29, 38, 74, 65, 82, 27, 89, 46] of size 9
If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.
Related Java Programs: