VOOZH about

URL: https://www.geeksforgeeks.org/java/vector-addall-method-in-java/

⇱ Vector addAll() Method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Vector addAll() Method in Java

Last Updated : 5 Dec, 2024

In Java, the addAll() method is used to add all elements from a specified Collection to the current Vector. The addAll() method allows you to efficiently append multiple elements to a vector in one operation.

Implementation:


Output
Vector : [1, 2, 3]

Now there are two versions of Vector addAll() method i.e one with the specified index and one without any index.

1. 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. 

Syntax:

boolean addAll(Collection C)

Parameters: The method accepts a mandatory parameter C which is a collection of ArrayList. It is the collection whose elements are needed to be appended at the end of the vector. 

Return Value: The method returns True if at least one action of append is performed, else False

Below program illustrate the Java.util.Vector.addAll() method: 


Output
The vector is: [Geeks, for, Geeks]
The new vector is: [Geeks, for, Geeks, A, Computer, Portal, for, Geeks]


2. 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. 

Syntax:

boolean addAll(int index, Collection C)

Parameters: This function accepts two parameters as shown in the above syntax and are described below.

  • index: This parameter is of integer datatype and specifies the position in the vector starting from where the elements from the container will be inserted.
  • C: It is a collection of ArrayList. It is the collection whose elements are needed to be appended.

Example:


Output
Vector : [1, 2, 3, 4]



Comment