Sunday, 10 May 2015

How to add all elements of a list to ArrayList

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class AddAllElement {

    public static void main(String[] args) {
       
        ArrayList<String> arr = new ArrayList<String>();
       
        // add element in arraylist
        arr.add("c");
        arr.add("php");
        arr.add("html");
        arr.add("java");
       
        // print arraylist
        System.out.println("arrayList is = " + arr);
       
        List<String> brr = new ArrayList<String>();
        brr.add("one");
        brr.add("second");
       
        //Appends all of the elements in the specified collection
        //to the end of this list, in the order that they are
        //returned by the specified collection's Iterator.
        arr.addAll(brr);
       
        System.out.println("After AddAll list brr ="+arr);
       
        List<String> linkList = new LinkedList<String>();
        linkList.add("Delhi");
        linkList.add("Kurukshetra");
   
        arr.addAll(linkList);
      
        System.out.println("After AddAll list linklist"+arr);
    }
   
}

Output:

arrayList is = [c, php, html, java]

After AddAll list brr =[c, php, html, java, one, second]

After AddAll list linklist[c, php, html, java, one, second, Delhi, Kurukshetra]



Related Posts: 

 


Storing Java Object In ArrayList







No comments:

Post a Comment