Skip to content
Related Articles

Related Articles

How to Insert all the Collection Elements to the Specified Position in Java ArrayList?
  • Difficulty Level : Medium
  • Last Updated : 07 Jan, 2021

The element can be inserted at the collection elements to the specified position in ArrayList using Collection.addAll() method which is present in java.util.ArrayList class. If any element present at the index then that element and all its right side elements are shifted to the right side. this method accepts two arguments the first argument is the index where we want to insert the elements and the second argument is the object of another collection. This method returns a boolean value true if elements are successfully inserted else returns false.

addAll(Collection c) : This method appends all of the elements in the specified collection to the end of 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(implies that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty).


public boolean addAll(int i, Collection c);

Exception: This method throws NullPointerException if elements contain one or more null values and c does not permit null elements, or if c or elements are null

Exception: Here two types of exceptions are possible.

  1. IndexOutOfBoundsException − If the index is out of range then it will show Index Out of Bounds Exception.
  2. NullPointerException − If the specified collection is null then it will show Null Pointer Exception.







// Java Program to insert Collection element at
// specified index
// Importing ArrayList class and Vector class 
// of java.utill package
import java.util.ArrayList;
import java.util.Vector;
public class GFG {
  // Main driver method
    public static void main(String[] args)
        // Create an ArrayList
        ArrayList<String> ArrList = new ArrayList<String>();
        // Adding elements in above ArrayList created
      // Custom inputs
        // Dispaly message
        System.out.print("The ArrayList is : ");
        // Display original ArrayList
        // Creating a vector for elements
        Vector<String> vector = new Vector<String>();
        // Insert elements in vector
      // Custom inputs
        // Note: Chosen index where added may vary as per
        // req Add vector element in arrayList at 1 index
        ArrList.addAll(1, vector);
        // Display arrayList after insert elements
        // at specific indexchosen above
            "List after addition of element at specifc index : ");
        // Display List after adding element at specific
        // index



The ArrayList is : [Computer, Science, Portal, GeeksforGeeks]
List after addition of element at specifc index : 
[Computer, x, y, z, Science, Portal, GeeksforGeeks]

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :