ArrayList Addall Method Tutorial

boolean addAll(Collection<? extends E> c)
boolean addAll(int index, Collection<? extends E> c)

The addAll() method is very useful for appending and inserting various array lists into the current ArrayList instance. There are two overloaded versions of the addAll() method. The first version has only one parameter and it appends a Collection object to the end of the current ArrayList instance. The documentation syntax for the parameter list (Collection<? extends E> c) may appear somewhat confusing. There are three parts to the documentation for this one single parameter. The first part is the class or interface - in this case the Collection interface - and the third part is the local parameter variable which will always be a reference variable in this case. Let's simplify things with a simple comparison,
myMethod(String s) - takes a String object and s is the local parameter variable which points to a string object.
myMethod(Collection c) - takes a subclass/subinterface reference of the Collection interface and c is the local parameter variable which points to subclass object derived from Collection.
The second part <? extends E> is generic syntax and it means that we can specify a type variable and all elements of the array being passed in will all be E or some subclass of E.
I briefly discussed the Collections Framework in my ArrayList Constructors Tutorial. ArrayList implements the very widely used List interface. The List interface inturn implements the Collection interface which explains the inheritance hierarchy between ArrayList and Collection. I won't go into too many confusing details at the moment, but just understand that we can pass a List type reference as an argument because List is a subinterface of Collection and a superinterface of ArrayList.
For example,

String sArray[] = { "Alaska", "Colorado", "Kentucky", "Ohio", "Texas", "Vermont" }; // ordinary good old array
List<String> list = Arrays.asList(sArray);
ArrayList<String> myList = new ArrayList<String>();
myList.add("Some of my favorite states");
myList.addAll(list);
System.out.println(myList);
// [Some of my favorite states, Alaska, Colorado, Kentucky, Ohio, Texas, Vermont]

The second version of addAll() takes two parameters, the first parameter is the index number to insert the Collection compatible array at. The second parameter is a reference variable to the Collection compatible array. Building on the ArrayList instance from the example above, I will demonstrate how to insert the parameter array.

// myList currently contains [Some of my favorite states, Alaska, Colorado, Kentucky, Ohio, Texas, Vermont]
myList.addAll(3, Arrays.asList("Delaware", "Florida", "Hawaii")); // insert between Colorado and Kentucky
System.out.println(myList);
// [Some of my favorite states, Alaska, Colorado, Delaware, Florida, Hawaii, Kentucky, Ohio, Texas, Vermont]



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md ArrayListAddAll
C:\Java>cd ArrayListAddAll
C:\Java\ArrayListAddAll>Notepad ArrayListAddAll.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


import java.util.*;

class ArrayListAddAll {
    public static void main(String args[]) {
        String sArray[] = { "Alaska", "Colorado", "Kentucky", "Ohio", "Texas", "Vermont" }; // ordinary good old array
        List<String> list = Arrays.asList(sArray);
        ArrayList<String> myList = new ArrayList<String>();
        myList.add("Some of my favorite states");
        myList.addAll(list);
        System.out.println(myList); 

        myList.addAll(3, Arrays.asList("Delaware", "Florida", "Hawaii")); 
        System.out.println(myList);
        System.out.println();

        ArrayList<Number> numList = new ArrayList<>();
        numList.add(new Double(6.99));
        numList.addAll(Arrays.asList(7, 10.3, 199, 1400.999, 41, 7.65f)); // autoboxing to primitive wrapper objects
        for(Number n : numList) {
            System.out.println(n + ", class type = " + n.getClass()); // getClass() - inherited from Object
        }
        System.out.println();

        ArrayList<RuntimeException> exList = new ArrayList<>();
        exList.addAll(Arrays.asList(new NullPointerException(), new IndexOutOfBoundsException(), new ClassCastException()));
        System.out.println(exList);
        System.out.println();

        // extra credit
        ArrayList<String> strList = new ArrayList<>();
        Collections.addAll(strList, "Blue", "Green", "Red", "Yellow", "Orange");
        System.out.println(strList);
    }
}

Now switch back to the command prompt (CMD) and type in javac ArrayListAddAll.java and press Enter.
Now type in java ArrayListAddAll and press Enter.


C:\Java\ArrayListAddAll>javac ArrayListAddAll.java
C:\Java\ArrayListAddAll>java ArrayListAddAll
[Some of my favorite states, Alaska, Colorado, Kentucky, Ohio, Texas, Vermont]
[Some of my favorite states, Alaska, Colorado, Delaware, Florida, Hawaii, Kentucky, Ohio, Texas, Vermont]

6.99, class type = class java.lang.Double
7, class type = class java.lang.Integer
10.3, class type = class java.lang.Double
199, class type = class java.lang.Integer
1400.999, class type = class java.lang.Double
41, class type = class java.lang.Integer
7.65, class type = class java.lang.Float

[java.lang.NullPointerException, java.lang.IndexOutOfBoundsException, java.lang.ClassCastException]

[Blue, Green, Red, Yellow, Orange]


Final thoughts

The addAll method is very useful for populating an ArrayList in bulk. In future ArrayList method tutorials I will keep on introducing more aspects of the Collection Framework.


Tutorials