Sunday, 11 May 2014

LinkedList : addAll(int index, Collection c) : Add Collection of data from specific index

public boolean addAll(int index, Collection<? extends E> c)
Inserts all of the elements in the specified collection into this list at the specified position. Shifts the element currently at that position and any subsequent elements to the right.

import java.util.*;
class LinkedListAddAllFromIndex{
 public static void main(String args[]){
  LinkedList<Integer> myList = new LinkedList<> ();
  Set<Integer> mySet =new HashSet<> ();
  
  /* Add Elements to myList */
  for(int i=0; i < 10; i++){
   myList.add(i);
  }
  
  /* Add Elements to mySet */
  for(int i=5; i < 15; i++){
   mySet.add(i);
  }
  
  System.out.println("Elements in myList are ");
  System.out.println(myList +"\n");
  System.out.println("Elements in mySet are ");
  System.out.println(mySet +"\n");
  
  /* Add mySet Data to myList from index 1 */
  myList.addAll(1, mySet);
  
  System.out.println("Elements in myList after adding mySet are");
  System.out.println(myList); 
 }
}

Output
Elements in myList are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in mySet are
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Elements in myList after adding mySet are
[0, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 1, 2, 3, 4, 5, 6, 7, 8, 9]

1. Throws IndexOutOfBoundsException, If index out of range
import java.util.*;
class LinkedListAddAllFromIndexOut{
 public static void main(String args[]){
  LinkedList<Integer> myList = new LinkedList<> ();
  Set<Integer> mySet =new HashSet<> ();
  
  /* Add Elements to myList */
  for(int i=0; i < 10; i++){
   myList.add(i);
  }
  
  /* Add Elements to mySet */
  for(int i=5; i < 15; i++){
   mySet.add(i);
  }
  
  System.out.println("Elements in myList are ");
  System.out.println(myList +"\n");
  System.out.println("Elements in mySet are ");
  System.out.println(mySet +"\n");
  
  System.out.println("About to add Elements at index -1");
  /* Add mySet Data to myList from index -1 */
  myList.addAll(-1, mySet);
 }
}

Output
Elements in myList are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in mySet are
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

About to add Elements at index -1
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: -1, Size: 10
        at java.util.LinkedList.checkPositionIndex(LinkedList.java:558)
        at java.util.LinkedList.addAll(LinkedList.java:404)
        at LinkedListAddAllFromIndexOut.main(LinkedListAddAllFromIndexOut.java:24)

2. Throws NullPointerException if the specified collection is null
import java.util.*;
class LinkedListAddAllFromIndexNull{
 public static void main(String args[]){
  LinkedList<Integer> myList = new LinkedList<> ();
  Set<Integer> mySet =null;
  
  /* Add Elements to myList */
  for(int i=0; i < 10; i++){
   myList.add(i);
  }
  
  System.out.println("Elements in myList are ");
  System.out.println(myList +"\n");
  System.out.println("Elements in mySet are ");
  System.out.println(mySet +"\n");
  
  System.out.println("About to add Elements at index 1");
  /* Add mySet Data to myList from index 1 */
  myList.addAll(1, mySet);
 }
}

Output
Elements in myList are
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Elements in mySet are
null

About to add Elements at index 1
Exception in thread "main" java.lang.NullPointerException
        at java.util.LinkedList.addAll(LinkedList.java:406)
        at LinkedListAddAllFromIndexNull.main(LinkedListAddAllFromIndexNull.java:19)


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment