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]] |

[ 0 ] Comments