Javatpoint Logo
Javatpoint Logo

Java Vector addAll() Method

The addAll() Java Vector class method inserts all of the elements in the specified collection to the end of the vector which is in use. The order of the elements will be the same as they are returned by the specified collection's iterator. There is two different types of Java addAll() method which can be differentiated depending on its parameter. These are:

  1. Java Vector addAll(Collection<? extends E> c) Method
  2. Java Vector addAll(int index, Collection<? extends E> c) Method

addAll(Collection<? extends E> c) Method:

This method is used to append all of the elements in the specified collection to the end of this Vector.

addAll(int index, Collection<? extends E> c) Method:

This method is used to insert all of the elements in the specified collection into the vector which is in use at the specified position.

Syntax:

Following is the declaration of addAll() method:

Parameter:

Parameter Description Required/Optional
index It is the index at which we will insert the first element from the specified collection. Required
c It is an element which will be inserted into this Vector. Required

Returns:

The addAll() method returns true if this Vector changed as a result of the call.

Exceptions:

NullPointerException- This method has thrown an exception if the specified collection is null.

IndexOutOfBoundsException- This method has thrown an exception if the index of an array is out of range i.e. (index < 0 || index > size()).

Compatibility Version:

Java 1.2 and above

Example 1:

Test it Now

Output:

List of first arrayList: [1, 2, 3]
List of second arrayList: [4, 5, 6, 7]
Final arrayList: [1, 2, 3, 4, 5, 6, 7]

Example 2:

Test it Now

Output:

Final vector list: [A, B, C, D, E, F, G, H]

Example 3:

Test it Now

Output:

Final vector list: [Java, Android, Python, PHP]

Example 4:

Test it Now

Output:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 3
	at java.base/java.util.Vector.addAll(Vector.java:1064)
	at myPackage.VectorAddAllExample4.main(VectorAddAllExample4.java:16)

Next TopicJava Vector





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA