4. boolean addAll(int index, Collection c)

4. boolean addAll(int index, Collection c)
Inserts all of the elements in the specified collection into this list,
starting at the specified position.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.spy.method.LinkedList;
 
import java.util.ArrayList;
import java.util.LinkedList;
 
public class AddAllThroughindex 
{
	public static void main(String[] args) 
 
	{
 
		LinkedList<String> list1=new LinkedList<String>();
 
      list1.add("Bhavesh");
      list1.add("Abhilash");
 
 
      //retrieving list1 element
 
      System.out.println("Retrieving list 1 element");
      for (String s : list1) 
      {
 
      	System.out.println(s);
 
      }
 
 
      LinkedList<String> list2=new LinkedList<String>();
 
      list2.add("Vivek");
      list2.add("milan");
 
      //Adding collection into list 2 at index 0
 
      list2.addAll(0,list1);
 
      System.out.println("Retrieving list 2:");
 
      for (String s : list2) 
      {
 
      	System.out.println(s);
 
	  }
 
	}
}

output

1
2
3
4
5
6
7
8
Retrieving list 1 element
Bhavesh
Abhilash
Retrieving list 2:
Bhavesh
Abhilash
Vivek
milan

Leave a Reply

Your email address will not be published. Required fields are marked *