The addAll() method of java.util.AbstractList class is used to insert all of the elements in the specified collection into this list at the specified position.
Java
Java
Java
- This shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices).
- The new elements will appear in this list in the order that they are returned by the specified collection's iterator.
- The behavior of this operation is undefined if the specified collection is modified while the operation is in progress.
public boolean addAll(int index, Collection c)Parameters: This method takes the following argument as a parameter.
- index- the index at which to insert the first element from the specified collection
- c- the collection containing elements to be added to this list
- Return Value: This method returns true if this list changed as a result of the call.
-
Exception: This method throws the following Exception.
- NullPointerException - if the specified collection contains one or more null elements and this list does not permit null elements, or if the specified collection is null
- IndexOutOfBoundsException - if the index is out of range (index size()).
// Java program to demonstrate
// addAll() method
// for String value
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// Creating object of AbstractList<String>
AbstractList<String>
arrlist1 = new ArrayList<String>();
// Populating arrlist1
arrlist1.add("A");
arrlist1.add("B");
arrlist1.add("C");
arrlist1.add("D");
arrlist1.add("E");
// print arrlist1
System.out.println("Original ArrayListlist : "
+ arrlist1);
// Creating another object of AbstractList<String>
AbstractList<String>
arrlist2 = new ArrayList<String>();
// Populating arrlist2
arrlist2.add("X");
arrlist2.add("Y");
arrlist2.add("Z");
// print arrlist2
System.out.println("ArrayList elements "
+ "to be added : "
+ arrlist2);
// adding the specified element
// starting from index 2
// using addAll() method
boolean value = arrlist1.addAll(2, arrlist2);
// print the value
System.out.println("Operation successful : "
+ value);
// print the new arrlist
System.out.println("New ArrayList : "
+ arrlist1);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
catch (IndexOutOfBoundsException e) {
System.out.println("Exception thrown : " + e);
}
}
}
Output:
Example 2: For NullPointerException
Original ArrayListlist : [A, B, C, D, E] ArrayList elements to be added : [X, Y, Z] Operation successful : true New ArrayList : [A, B, X, Y, Z, C, D, E]
// Java program to demonstrate
// addAll() method
// for NullPointerException
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// Creating object of AbstractList<String>
AbstractList<String>
arrlist1 = new ArrayList<String>();
// Populating arrlist1
arrlist1.add("A");
arrlist1.add("B");
arrlist1.add("C");
arrlist1.add("D");
arrlist1.add("E");
// print arrlist1
System.out.println("Original ArrayListlist : "
+ arrlist1);
// Creating another object of AbstractList<String>
AbstractList<String> arrlist2 = null;
// printing the arrlist2
System.out.println("ArrayList to be added : "
+ arrlist2);
// adding the specified element
// starting from index 2
// using addAll() method
System.out.println("\nTrying to get"
+ " the null collection");
boolean value = arrlist1.addAll(2, arrlist2);
// print the value
System.out.println("operation successful : "
+ value);
// print the new arrlist
System.out.println("New ArrayList : "
+ arrlist1);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
catch (IndexOutOfBoundsException e) {
System.out.println("Exception thrown : " + e);
}
}
}
Output:
Example 3: For IndexOutOfBoundsException
Original ArrayListlist : [A, B, C, D, E] ArrayList to be added : null Trying to get the null collection Exception thrown : java.lang.NullPointerException
// Java program to demonstrate
// addAll() method
// for IndexOutOfBoundsException
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
throws Exception
{
try {
// Creating object of AbstractList<String>
AbstractList<String>
arrlist1 = new ArrayList<String>();
// Populating arrlist1
arrlist1.add("A");
arrlist1.add("B");
arrlist1.add("C");
arrlist1.add("D");
arrlist1.add("E");
// print arrlist1
System.out.println("Original ArrayListlist : "
+ arrlist1);
// Creating another object of AbstractList<String>
AbstractList<String>
arrlist2 = new ArrayList<String>();
// Populating arrlist2
arrlist2.add("X");
arrlist2.add("Y");
arrlist2.add("Z");
// print arrlist2
System.out.println("ArrayList elements to be added : "
+ arrlist2);
// adding the specified element
// starting from index 2
// using addAll() method
System.out.println("\nTrying to put the out"
+ " of range index ");
boolean value = arrlist1.addAll(-1, arrlist2);
// print the value
System.out.println("operation succecfull : "
+ value);
// print the new arrlist
System.out.println("New ArrayList : " + arrlist1);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
catch (IndexOutOfBoundsException e) {
System.out.println("Exception thrown : " + e);
}
}
}
Output:
Original ArrayListlist : [A, B, C, D, E] ArrayList elements to be added : [X, Y, Z] Trying to put the out of range index Exception thrown : java.lang.IndexOutOfBoundsException: Index: -1, Size: 5