Java

The Java Add Method for ArrayList

ArrayList is a class of the list interface. It is a collection. An ArrayList object can be constructed in two main ways: with the ArrayList empty or with the ArrayList having elements. When it is constructed empty, elements have to be added (included) into it. This is done with the add() method of the ArrayList object. The add() method can be used to prepend an element, to insert an element and to append an element to the ArrayList object. This article explains how to use the add() method for the ArrayList object. It begins with the constructor of the empty ArrayList object.

Constructing an ArrayList Object

The ArrayList is from the java.util package. The syntax to construct an empty ArrayList is,

ArrayList<T> al = new ArrayList<T>();

where T in angle brackets is the type of value wanted for the list. T is on both sides of the assignment operator. Note the positions. al is the ArrayList object. The following program creates an empty ArrayList object:

    import java.util.*;
    public class TheClass {
        public static void main(String[] args) {
            ArrayList al = new ArrayList();
            int len = al.size();
            System.out.println(len);
        }
    }

The output is: 0. It is a list of chars. ArrayList uses a reference as the type and not a primitive name. So “Character” should be used, instead of “char”.

Appending Elements

When the ArrayList object is empty, values can be added. That is appending. When the ArrayList object already has some values, adding more values at the end, is still appending. The syntax of the ArrayList add() method for appending is:

public boolean add(E e)

It returns true if a change was made in the ArrayList object; and false, if no change was made. E is the value.

The following code segment appends four characters to the ArrayList object, using the add() method:

    import java.util.*;
    public class TheClass {
        public static void main(String[] args) {
            ArrayList al = new ArrayList();
            al.add('P'); al.add('Q'); al.add('S'); al.add('T');

            for (int i=0; i<al.size(); i++) {
                System.out.print(al.get(i)); System.out.print(' ');
            }
            System.out.println();
        }
    }

The output is:

P Q S T

Notice that get() and not the square brackets, was used to obtain the value at the index.

Inserting an Element

The syntax to insert an element using the add method, is:

public void add(int index, E element)

index is the position, where the element is added. Any element to the right is shifted one place ahead. The following program illustrates this:

    import java.util.*;
    public class TheClass {
        public static void main(String[] args) {
            ArrayList al = new ArrayList();
            al.add('P'); al.add('Q'); al.add('S'); al.add('T');

            al.add(2, 'R');

            for (int i=0; i<al.size(); i++) {
                System.out.print(al.get(i)); System.out.print(' ');
            }
            System.out.println();
        }
    }

The output is:

P Q R S T

‘R’ has been inserted at index 2, where ‘S’ was. Index 2 is the third position. ‘S’ and ‘T’ were each shifted, one place ahead.

Prepending

To prepend, means to add in front. To prepend, use the syntax:

public void add(int index, E element)

but with the index at 0.

The following program illustrates this:

    import java.util.*;
    public class TheClass {
        public static void main(String[] args) {
            ArrayList al = new ArrayList();
            al.add('P'); al.add('Q'); al.add('S'); al.add('T');

            al.add(0, 'Z');

            for (int i=0; i<al.size(); i++) {
                System.out.print(al.get(i)); System.out.print(' ');
            }
            System.out.println();
        }
    }

The output should be:

Z P Q S T

Z has been prepended to P Q S T.

Appending Another List

A whole list can be appended. The syntax for this action is:

public boolean addAll(Collection<? extends E> c)

where c is the list to be appended. It returns true if a change was made in the ArrayList object; and false, if no change was made.

In the following main method, there are two lists: al and another. another is appended to al.

        public static void main(String[] args) {
            ArrayList al = new ArrayList();
            al.add('P'); al.add('Q'); al.add('R'); al.add('S');
            ArrayList another = new ArrayList();
            al.add('T'); al.add('U');

            al.addAll(another);

            for (int i=0; i<al.size(); i++) {
                System.out.print(al.get(i)); System.out.print(' ');
            }
            System.out.println();
        }

The output is:

P Q R S T U

al has the sequence, P Q R S. another has the sequence T U. T U has clearly been appended to P Q R S.

Inserting Another List

A whole list can be inserted into a first list. The syntax for this is:

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

where c is the list to be inserted. It returns true if a change was made in the ArrayList object; and false, if no change was made.

In the following main() method, there are two lists: al and another. another is inserted within al.

        public static void main(String[] args) {
            ArrayList al = new ArrayList();
            al.add('P'); al.add('Q'); al.add('T'); al.add('U');
            ArrayList another = new ArrayList();
            al.add('R'); al.add('S');

            al.addAll(2, another);

            for (int i=0; i<al.size(); i++) {
                System.out.print(al.get(i)); System.out.print(' ');
            }
            System.out.println();
        }

The output is:

P Q R S T U

Here, al has the sequence, P Q T U. Here, another has the sequence R S. R S has clearly been inserted into P Q T U to have P Q R S T U.

Prepending Another List

A whole list can be prepended into a first list. The syntax for this is still,

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

but choosing 0 for the index. It returns true if a change was made in the ArrayList object; and false, if no change was made.

In the following main method, there are two lists: al and another. another is inserted in front of al.

        public static void main(String[] args) {
            ArrayList al = new ArrayList();
            al.add('W'); al.add('X'); al.add('Y'); al.add('Z');
            ArrayList another = new ArrayList();
            al.add('U'); al.add('V');

            al.addAll(0, another);

            for (int i=0; i<al.size(); i++) {
                System.out.print(al.get(i)); System.out.print(' ');
            }
            System.out.println();
        }

The output is:

U V W X Y Z

Here, al has the sequence, W X Y Z. Another has the sequence U V. U V has clearly been inserted in front of W X Y Z to have U V W X Y Z.

Conclusion

The add() method of ArrayList can be used to prepend, insert, and append an element or another list. To append just one element, use add(e). To insert just one element, use add(index, e). To prepend just one element, make the index, 0. To append another list, use addAll( c). To insert another list, use addAll(index, c). To prepend another list, make the index, 0.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.