# Insert all elements of other Collection to Specified Index of Java ArrayList

Java 8Object Oriented ProgrammingProgramming

The elements of a Collection can be inserted at the specified index of the ArrayList using the method java.util.ArrayList.addAll(). This method has two parameters i.e. the specific index at which to start inserting the Collection elements in the ArrayList and the Collection itself.

If there is an element already present at the index specified by ArrayList.addAll() then that element and all subsequent elements are shifted to the right to make the space for the Collection elements in the ArrayList.

A program that demonstrates this is given as follow.

## Example

Live  Demo

import java.util.ArrayList;
import java.util.Vector;
public class Demo {
public static void main(String[] args) {
ArrayList<String> aList = new ArrayList<String>();
Vector<String> vec = new Vector<String>();
System.out.println("The ArrayList elements are: " + aList);
}
}

## Output

The output of the above program is as follows

The ArrayList elements are: [John, Sally, Peter, Michael, Jenna, Harry, Martha, Susan]

Now let us understand the above program.

First, the ArrayList is defined and elements are added using the ArrayList.add() method. A code snippet which demonstrates this is as follows

ArrayList<String> aList = new ArrayList<String>();
aList.add("Susan");
Vector<String> vec = new Vector<String>();
System.out.println("The ArrayList elements are: " + aList);