Javatpoint Logo
Javatpoint Logo

Java ArrayList addAll() method

Java ArrayList addAll(Collection c) method

The addAll (Collection c) method of Java ArrayList classappends all of the elements in the specified collection to the end of this list. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

Syntax:

Parameter:

"c": is the Collection that contained elements to be added to this list.

Return:

Return "true": if the collection "c" has been appended successfully to the original list.

Exception:

java.lang.NullPointerException: If the specified collection "c" is null.

Example 1

Test it Now

Output:

[A, B, C]

[Apple, Ball, Cat]

true

[A, B, C, Apple, Ball, Cat]

Example 2

Test it Now

Output:

[Apple, Banana, Orange]

Java ArrayListaddAll(int index, Collection c) method

The addAll (int index, Collection c) method of Java ArrayList classinserts all of the elements in the specified collectioninto this list starting at the specified index.

It shifts the element currently at that position and any subsequent elements to the right. The new elements will appear in the list in the order that they are returned by the specified collection?s iterator.

Syntax:

Parameter:

"index":The position at which to insert the first element of the specified list.

"c": is the Collection that contained elements to be added to this list.

Return:

Return "true": If the collection "c" has been inserted successfully to the original list.

Exception:

java.lang.NullPointerException: If the specified collection "c" is null.

java.lang.IndexOutOfBoundsException: If the index is out of range.

Example 3

Test it Now

Output:

[A, B, C]
[Adel, Ahmed, Ali]
true
[A, Adel, Ahmed, Ali, B, C]

Example 4

Test it Now

Output:

[1, 2, 3]

3

java.lang.IndexOutOfBoundsException: Index: 4, Size: 3

Example 5

Test it Now

Output:

[1, 2, 3]

3
java.lang.NullPointerException

Next TopicJava ArrayList




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA