How to use the ArrayList class

Description:

  • An ArrayList is a collection that's imilar to an array, but can change its capacity as elements are added or removed. The ArrayList class uses an array to store the elements it contains.
  • You can specify the type of elements to be stored in the array list by naming a type in the angle brackets.
  • You can specify the size of an arraylist when you create it, or you can let the ArrayList default to an inital capacity of 10 elements.
  • The capacity of an ArrayList automatically increases whenever necessary.

Constructor Description
ArrayList<E>() Creates an empty ArrayList with an inital capacity of 10 objects of the specific type.
ArrayList<E>(initCapacity) Creates an empty ArrayList with with a specific capacity.
ArrayList<E>(Collection) Creates an ArrayList containing the elements of the specified collection.

Method Description
add(object) Adds to specified object to the end of the list.
add(idex, object) Adds the specified object at the specified index position.
clear() Removes all elements from the list.
contains(object) Returns true if the spedified object is in the list.
get(index) Return the object at the specified index position.
indexOf(object) Return the index position of the specified object.
isEmpty() Returns true if the list is empty.
remove(index) Removes the object at the specified index.
remove(object) Removes the specified object.
set(index, object) Sets the element at the specified index to the specified object.
size() Returs the number of elements in the list.
toArray() Returns an array containing the elements of the list.