addAll(int index, Collection c) method of List Interface in java.

addAll(int index, Collection c) method of List Interface in java.


Posted in : Core Java Posted on : January 3, 2011 at 7:50 PM Comments : [ 0 ]

In this method you can insert a specified collection at the specific position into a list.

addAll(int index, Collection c) method of List Interface in java.

In the following example, we will show you the method of adding all elements of a specified collection (list) in the specified position at the index in list using addAll(int index, Collection c) method of List Interface in java. 

Syntax

boolean addAll(int index, Collection c)

This method returns either True or False value. 

In this method you can insert a specified collection at the specific position into a list. It inserts all elements of the specified collection at the current position and replace the new collection with old element and shifts the old element to it's right by increasing its index, if any element is positioned at that index. An element of a  collection is to be added by this method to the specified collection in the order in which they are returned and the new elements will also be appear in the order they are returned. The behavior of this operation is uninfluenced if the specified collection is modified while the operation is in progress.

parameter description:-

int index : It takes an argument as integer value onto which place (index) do you want to add a collection.  

Collection c : It takes an argument as collection (List) which do you want to add to the list.

Example of addAll(int index, Collection c) method

In this example we will show you how does an addAll(int index, Collection c) method work in List interface. This Example will help you to understand  how a collection can be added at the specific position into the list. Through this example we will show you how all elements of a list can be added to a list at the specified position, and count the number of elements from list before and after adding the elements.

Example:-

package Devmanuals.com;
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
public class ListAddAllIndex {
public static void main (String args [] ){
List al =new ArrayList();
al.add("A");
al.add("B");
al.add("E");
System.out.println("Elements of list1 are : " + al);
System.out.println("Size of list1 is : "+ al.size());
List ll=new LinkedList();
ll.add("C");
ll.add("D");
System.out.println("Elements of list2 are : "+ ll);
System.out.println("Size of list2 are : "+ ll.size());
boolean bl= al.addAll(2,ll);
System.out.println("The list after addition : "+al);
System.out.println("Is the value added to the list TRUE/FALSE : "+bl);
System.out.println("Size of list after addition : "+al.size());
}
}

Output:-

Elements of list1 are : [A, B, E]
Size of list1 is : 3
Elements of list2 are : [C, D]
Size of list2 are : 2
The list after addition : [A, B, C, D, E]
Is the value added to the list TRUE/FALSE : true
Size of list after addition : 5

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics