Insert all elements of other Collection to Specified Index of Java LinkedList

Insert all elements of other Collection to Specified Index of Java LinkedList


Posted in : Java Posted on : November 29, 2010 at 5:51 PM Comments : [ 0 ]

This Java Example shows how to insert all elements of other collection object at specified index of LinkedList object in java.

Insert all elements of other Collection to Specified Index of Java LinkedList Example

This Java Example shows how to insert all elements of other collection object at specified index of LinkedList object in java. To insert all element of other collection we use boolean addAll(int index,Collection obj) method. This method insert all of the elements in the specified collection to the Specified Index of this list. It returns true if the LinkedList was changed by the method call.

 Example: InsertAllElementCollectionAtIndexLinkedList.java

package devmanuals.com;

import java.util.*;

public class InsertAllElementCollectionAtIndexLinkedList {
	public static void main(String[] args) {

		LinkedList LKLST = new LinkedList();
		LKLST.add("Gyan");
		LKLST.add("Singh");
		LKLST.add("Arunesh");
		LKLST.add("Kumar");

		ArrayList ALST = new ArrayList();
		ALST.add("1");
		ALST.add("2");

		System.out.println("The LinkedList Elements are : " + LKLST);
		LKLST.addAll(2, ALST);
		System.out.println("The LinkedList Elements are : " + LKLST);
		LKLST.add(ALST);// insert all element at a single index
		System.out.println("The LinkedList Elements are : " + LKLST);

	}
}

Output:

The LinkedList Elements are : [Gyan, Singh, Arunesh, Kumar] 

The LinkedList Elements are : [Gyan, Singh, 1, 2, Arunesh, Kumar] 

The LinkedList Elements are : [Gyan, Singh, 1, 2, Arunesh, Kumar, [1, 2]]

Download The Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics