Java ArrayList addAll Example

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
 * version 14 May 2008
 * author Rose India 
 */
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]

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US