ArrayList Class

The ArrayList class extends AbstractList and implements the List interface.

ArrayList supports dynamic arrays that can grow as needed.
An ArrayList is a variable-length array of object references.
An ArrayList can dynamically increase or decrease in size.

ArrayList is a generic class that has declaration of:

class ArrayList<E>

E specifies the type of objects that the list will hold.

A demonstration of an array-based list

 
import java.util.ArrayList;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    List<String> ls = new ArrayList<String>();
    String[] weekDays = { "A", "B", "C", "Wed", "Z", "Y", "X" };
    for (String weekDay : weekDays){
      ls.add(weekDay);
    }
    dump("ls:", ls);
    ls.set(ls.indexOf("Wed"), "Wednesday");
    dump("ls:", ls);
    ls.remove(ls.lastIndexOf("X"));
    dump("ls:", ls);
  }
  static void dump(String title, List<String> ls) {
    System.out.print(title + " ");
    for (String s : ls){
      System.out.print(s + " ");
    }
      
    System.out.println();
  }
}
  

This class is a member of the Java Collections Framework.

Constructors shown here:

ArrayList()
Creates an empty list with an initial capacity of ten.
ArrayList(Collection<? extends E> c)
Creates a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
ArrayList(int initialCapacity)
Creates an empty list with the specified initial capacity.

Add to ArrayList

boolean add(E e)
Appends the specified element to the end of this list.
void add(int index, E element)
Inserts the specified element at the specified position in this list.
boolean addAll(Collection<? extends E> c)
Appends all of the elements in the specified collection 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 of the elements in the specified collection into this list, starting at the specified position.
void clear()
Removes all of the elements from this list.
boolean contains(Object o)
Returns true if this list contains the specified element.
void ensureCapacity(int minCapacity)
Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
E get(int index)
Returns the element at the specified position in this list.
boolean isEmpty()
Returns true if this list contains no elements.
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
E remove(int index)
Removes the element at the specified position in this list.
boolean remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present.
E set(int index, E element)
Replaces the element at the specified position in this list with the specified element.
int size()
Returns the number of elements in this list.
void trimToSize()
Trims the capacity of this ArrayList instance to be the list's current size.
Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
<T> T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

Revised from Open JDK source code

Home 
  Java Book 
    Collection  

ArrayList:
  1. ArrayList Class
  2. Create ArrayList object
  3. Add to ArrayList
  4. Clear an ArrayList
  5. Shallow copy of current ArrayList
  6. If contain a certain object
  7. Increases the capacity of an ArrayList
  8. Get/Replace element by index
  9. Get object for index
  10. Remove from ArrayList
  11. Get the size and trim to size
  12. If ArrayList is empty
  13. Convert ArrayList to array