Constructing an ArrayList Object
The ArrayList is from the java.util package. The syntax to construct an empty ArrayList is,
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:
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:
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:
The output is:
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:
index is the position, where the element is added. Any element to the right is shifted one place ahead. The following program illustrates this:
The output is:
‘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:
but with the index at 0.
The following program illustrates this:
The output should be:
Z has been prepended to P Q S T.
Appending Another List
A whole list can be appended. The syntax for this action is:
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.
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:
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:
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.
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:
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,
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.
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:
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.