Monday 7 May 2012

Java ArrayList addAll Example

 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
ArrayList class addAll method example. This example shows you how to use addAll method.

ArrayList class addAll method example.public boolean addAll(int index, Collection c) 
Inserts all of the elements in the specified collection into this list, starting at the specified position.
Shifts the element currently at that position and any subsequent elements to the right. 
The new elements will appear in the list in the order that they are returned by the specified collection's iterator. 


Here is the code

/**
 * @ # AddAll.java
 * A class repersenting use to addAll(int index,collection c) 
 * method of ArrayList class in java.util package
 */
import java.util.*;

public class AddAll1 {

    public static void main(String args[]) {
        List arrlist = new ArrayList();
        List arrlist1 = new ArrayList();
        int i = 0;
        String str = "Rose India";

        //Add the specified element to the end of this list.
        arrlist.add(i);
        arrlist.add(str);
        System.out.println("arrlist = " + arrlist);

        //Add the specified element to the end of this list.
        arrlist1.add("Rose");
        arrlist1.add("India");
        System.out.println("arrlist1 = " + arrlist);

        //Insert all of the elements in the specified collection into the ArrayList  
        arrlist.addAll(1,arrlist1);
        System.out.println("arrlist = " + arrlist);
    }
}
output:
-------------------------
arrlist = [0, Rose India]
arrlist1 = [0, Rose India]
arrlist = [0, Rose, India, Rose India]

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...