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 (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that they are returned by the specified collection’s iterator.
Collections.addAll(l1, names); boolean b = Collections.addAll(l2, "Rahul", "OM", "Prem");
package com.candidjava; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class ArrayListAddArray { public static void main(String[] args) { String[] names = { "teena", "reena", "meena", "ram", "sam" }; // Using arrays.aslist() List l = new ArrayList<String>(Arrays.asList(names)); System.out.println("Adding into arraylist using arrays.aslist:"); System.out.println(l); // Using Collections.addall() List l1 = new ArrayList<String>(); Collections.addAll(l1, names); System.out.println("Adding into arraylist using collection.addall:"); System.out.println(l1); // Adding elements to an already existing array List l2 = new ArrayList<String>(); Collections.addAll(l2, names); boolean b = Collections.addAll(l2, "Rahul", "OM", "Prem"); System.out.println("Adding elements into list l2:"); System.out.println(l2); // Adding elements to array l1.add("krish"); l1.add("jack"); System.out.println("Adding elements into list l1:"); System.out.println(l1); } }
OUTPUT
Adding into arraylist using arrays.aslist: [teena, reena, meena, ram, sam] Adding into arraylist using collection.addall: [teena, reena, meena, ram, sam] Adding elements into list l2: [teena, reena, meena, ram, sam, Rahul, OM, Prem] Adding elements into list l1: [teena, reena, meena, ram, sam, krish, jack]