Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaHow to Insert all the Collection Elements to the Specified Position in...

How to Insert all the Collection Elements to the Specified Position in Java ArrayList?

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

Syntax:

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.

CODE:

Java




// Java Program to insert Collection element at
// specified index
 
// Importing ArrayList class and Vector class
// of java.util 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
        ArrList.add("Computer");
        ArrList.add("Science");
        ArrList.add("Portal");
        ArrList.add("Lazyroar");
 
        // Display message
        System.out.print("The ArrayList is : ");
 
        // Display original ArrayList
        System.out.println(ArrList);
 
        // Creating a vector for elements
        Vector<String> vector = new Vector<String>();
 
        // Insert elements in vector
      // Custom inputs
        vector.add("x");
        vector.add("y");
        vector.add("z");
 
        // 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
        System.out.println(
            "List after addition of element at specific index : ");
 
        // Display List after adding element at specific
        // index
        System.out.println(ArrList);
    }
}


Output

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

 

RELATED ARTICLES

Most Popular

Recent Comments