Home »
Java programming language
Java Vector addAll() method with Example
Vector Class addAll() method: Here, we are going to learn about the addAll() method of Vector Class with its syntax and example.
Submitted by Preeti Jain, on March 15, 2020
Vector Class addAll() method
Syntax:
public boolean addAll(Collection co);
public boolean addAll(int indices, Collection co);
- addAll() method is available in java.util package.
- addAll(Collection co) method is used to append all of the objects that exist in the given collection (co) at the last of this List.
- addAll(int indices, Collection co) method is used to append all of the objects that exist in the given collection (co) into this vector and the element insertion starts at the given indices.
-
These methods may throw an exception at the time of adding an element.
- IndexOutOfBoundsException: This exception may throw when the given parameter is not in a range.
- NullPointerException: This exception may throw when the given parameter is null exists.
- These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.
Parameter(s):
- In the first case, addAll(Collection co),
Collection co – represents the collection of elements to be appended.
-
In the first case, addAll(int indices, Collection co),
- int indices – represents the position of starting element to insert from the given collection.
- Collection co – represents the collection of elements to be appended.
Return value:
In both the cases, the return type of the method is boolean,
- In the first case, it returns true when all of the objects are appended in the given collection to the end of the list otherwise it returns false.
- In the second case, it returns true when all of the objects are to be appended in the given collection at the given indices successfully otherwise it returns false.
Example:
// Java program to demonstrate the example
// of addAll() method of Vector
import java.util.*;
public class AddAllOfVector {
public static void main(String[] args) {
// Instantiates a vector object
Vector < String > v = new Vector < String > (10);
ArrayList arr_l = new ArrayList(10);
// By using add() method is to add
// the elements in vector
v.add("C");
v.add("C++");
v.add("SFDC");
v.add("JAVA");
// By using add() method is to add
// the elements in arr_l
arr_l.add("SQL");
arr_l.add("DBMS");
//Display Vector and ArrayList
System.out.println("v: " + v);
System.out.println("arr_l: " + arr_l);
// By using addAll(arr_l) method is used
// to add all the given objects exists in
// the given collection will be appended
// to this vector at the last
v.addAll(arr_l);
// Display Vector
System.out.println("v.addAll(arr_l): " + v);
// By using addAll(arr_l,2) method is used
// to add all the given objects exists in
// the given collection will be appended
// to this vector at the given indices
v.addAll(2, arr_l);
// Display Vector
System.out.println("v.addAll(2,arr_l,): " + v);
}
}
Output
v: [C, C++, SFDC, JAVA]
arr_l: [SQL, DBMS]
v.addAll(arr_l): [C, C++, SFDC, JAVA, SQL, DBMS]
v.addAll(2,arr_l,): [C, C++, SQL, DBMS, SFDC, JAVA, SQL, DBMS]