An ArrayList
object has several important characteristics:
ArrayList
can contain only
class objects, not primitive types.
Fortunately, there are wrapper types,
e.g., Integer
, Float
, Double
,
and Boolean
. If you construct
an ArrayList
of one of these
then you can put values
of the associated primitive type in the list.
The compiler will automatically wrap the primitive type up as an
obect of its associated wrapper class (known as
auto-boxing. When you retrieve the object
(using the get
method, for example), the compiler will
automatically unwrap the primitive from its wrapper (known as
auto-unboxing). For example:
ArrayList<Integer> numberList = new ArrayList<Integer>(); numberList.add(42); int listItem = numberList.get(0);
size() | returns number of elements in list | |
isEmpty() | returns true if the list is empty; false otherwise | |
add(E element) | adds given element to end of list | |
add(int index, E element) | adds given element at specified index list, shifting later elements | |
get(int index) | returns element at specified index | |
set(int index, E element) | replaces the element currently at the specified index with the given element | |
remove(E element) | finds given element in list and removes it (first occurrence) | |
remove(int index) | removes element at specified index list, shifting later elements |