ArrayList Add Method Tutorial

boolean add(E e)
void add(int index, E element)

The add() method is very useful for appending and inserting elements into an ArrayList. There are two overloaded versions of the add() method. The first version has only one parameter and simply appends the parameter object to the end of the ArrayList. If you try to add an object that is not compatible with the class or interface specified as the type-variable, a compiler error will occur.
For example,
     ArrayList<StringBuilder> aList = new ArrayList<>();
     aList.add(new StringBuilder("Alaska"));
     aList.add(new StringBuilder("New York"));
     aList.add(new StringBuilder("Vermont"));
     System.out.println(aList);
// [Alaska, New York, Vermont]

In the example above you can plainly see that the add() simply appends the object parameter as a new element in the ArrayList. The ArrayList will dynamically grow to accommodate new elements as they are added. The second version takes two parameters, the first parameter is the index number to insert a new element at – ArrayList elements begin at index 0. The second parameter is simply the object element to insert. Building on the ArrayList instance from the example above, I will demonstrate how to add a couple of new records.
     // aList currently contains [Alaska, New York, Vermont]
     aList.add(1, new StringBuilder("Hawaii")); // insert between Alaska and New York
     aList.add(3, new StringBuilder("Ohio"));
// insert between New York and Vermont
     System.out.println(aList);
// [Alaska, Hawaii, New York, Ohio, 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 ArrayListAdd
C:\Java>cd ArrayListAdd
C:\Java\ArrayListAdd>Notepad ArrayListAdd.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.ArrayList;

class ArrayListAdd {
    public static void main(String args[]) {
        ArrayList<StringBuilder> aList = new ArrayList<>();
        aList.add(new StringBuilder("Alaska"));
        aList.add(new StringBuilder("New York"));
        aList.add(new StringBuilder("Vermont"));
        System.out.println(aList); 
        System.out.println();

        aList.add(1, new StringBuilder("Hawaii")); // insert between Alaska and New York
        aList.add(3, new StringBuilder("Ohio")); // insert between New York and Vermont
        System.out.println(aList); 
        System.out.println();

        ArrayList<RuntimeException> reList = new ArrayList<>();
        reList.add(new NullPointerException());
        reList.add(new StringIndexOutOfBoundsException());
        reList.add(new ClassCastException());
        reList.add(2, new IllegalArgumentException());
        System.out.println(reList);
        System.out.println();

        ArrayList<Number> nList = new ArrayList<Number>();
        nList.add(new Byte((byte)41));
        nList.add(new Integer(1024));
        nList.add(1, new Double(4.75));
        System.out.println(nList);
    }
}

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


C:\Java\ArrayListAdd>javac ArrayListAdd.java
C:\Java\ArrayListAdd>java ArrayListAdd
[Alaska, New York, Vermont]

[Alaska, Hawaii, New York, Ohio, Vermont]

[java.lang.NullPointerException, java.lang.StringIndexOutOfBoundsException, java.lang.IllegalArgumentException, java.lang.ClassCastException]

[41, 4.75, 1024]


Final thoughts

When specifying the type-variable inside the diamond syntax, you can add objects of that class or any subclass that is derived from that class.


Tutorials