ArrayList add/insert
In this chapter you will learn:
Add elements to an ArrayList
The following code add elements to an existing ArrayList
.
boolean add(E e)
Appends the end of this list.void add(int index, E element)
Inserts at the specified position in this list.boolean addAll(Collection<? extends E> c)
Appends all to the end of this list, in the order that they are returned by the specified collection's Iterator.boolean addAll(int index, Collection<? extends E> c)
Inserts all in the specified collection into this list, starting at the specified position.
import java.util.ArrayList;
// ja v a 2 s . c o m
public class Main {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList<String>();
al.add("C");
al.add("A");
al.add("E");
al.add("java2s.com");
al.add("D");
al.add("F");
al.add(1, "java2s.com");
System.out.println(al);
}
}
The output:
The last add methods inserts a string value at position 1.
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Collections