addAll(int index, Collection c)

addAll(int index, Collection c): This addall method in Java utilizes to appends all the elements of the specified collection at the specified position in ArrayList. Its return type is boolean. So, It inserts all the elements in the specified collection into this list, starting at the specified position, and Shifts the elements present at the current position.

boolean addAll(int index, Collection c);

Where index, index at which the specified element is to be inserted in addAll method in Java.
c, collection containing elements to be added to this list.
throws NullPointerException, if the specified collection is null.
Also, return type,  it’s return type is boolean.

import java.util.ArrayList;
public class ArraylistExample 
{
	public static void main(String[] args) 
        {
		ArrayList<String> listOfNames = new ArrayList<String>();
		listOfNames.add("JAVA");
		listOfNames.add("GOAL");
		listOfNames.add("RAVI");
			
		ArrayList<String> listOfNames2 = new ArrayList<String>();
		listOfNames2.add("NEW");
		listOfNames2.add("LIST");
		
		listOfNames.addAll(1, listOfNames2);
			
		for(String name : listOfNames)
		{
			System.out.println("Names from list = "+name);
		}
	}
}

Output: Names from list = JAVA
Names from list = NEW
Names from list = LIST
Names from list = GOAL
Names from list = RAVI

Leave a Comment