The ArrayList package (Package: java.util) in Java lets you create and maintain a special type of collection object: an array list. An array list is similar to an array but averts many of the most common problems of working with arrays, specifically the following:
An array list automatically resizes itself whenever necessary.
An array list lets you insert elements into the middle of the collection.
An array list lets you delete items.
Constructors
Constructor | Explanation |
---|---|
ArrayList() | Creates an array list with an initial capacity of ten elements. |
ArrayList(int capacity) | Creates an array list with the specified initial capacity. |
ArrayList(Collection c) | Creates an array list and copies all the elements from the specified collection into the new array list. |
Methods
Method | Explanation |
---|---|
add(Object element) | Adds the specified object to the array list. If you specified a type when you created the array list, the object must be of the correct type. |
add(int index, Object element) | Adds the specified object to the array list at the specified index position. If you specified a type when you created the array list, the object must be of the correct type. |
addAll(Collection c) | Adds all the elements of the specified collection to this array list. |
addAll(int index, Collection c) | Adds all the elements of the specified collection to this array list at the specified index position. |
clear() | Deletes all elements from the array list. |
clone() | Returns a shallow copy of the array list. The elements contained in the copy are the same object instances as the elements in the original. |
contains(Object elem) | Returns a Boolean value that indicates whether the specified object is in the array list. |
containsAll(Collection c) | Returns a Boolean value that indicates whether this array list contains all the objects that are in the specified collection. |
ensureCapacity(int minCapacity) | Increases the array list’s capacity to the specified value. (If the capacity is already greater than the specified value, this method does nothing.) |
get(int index) | Returns the object at the specified position in the list. |
indexOf(Object elem) | Returns the index position of the first occurrence of the specified object in the array list. If the object isn’t in the list, it returns -1. |
isEmpty() | Returns a Boolean value that indicates whether the array list is empty. |
iterator() | Returns an iterator for the array list. |
lastIndexOf(Object elem) | Returns the index position of the last occurrence of the specified object in the array list. If the object isn’t in the list, it returns -1. |
remove(int index) | Removes the object at the specified index and returns the element that was removed. |
remove(Object elem) | Removes an object from the list. Note that more than one element refers to the object; this method removes only one of them. It returns a Boolean value that indicates whether the object was in the list. |
remove(int fromIndex, int toIndex) | Removes all objects whose index values are between the values specified. Note that the elements at the fromIndex and toIndex positions are not themselves removed. |
removeAll(Collection c) | Removes all the objects in the specified collection from this array list. |
retainAll(Collection c) | Removes all the objects that are not in the specified collection from this array list. |
set(int index, Object elem) | Sets the specified element to the specified object. The element that was previously at that position is returned as the method’s return value. |
size() | Returns the number of elements in the list. |
toArray() | Returns the elements of the array list as an array of objects (Object[]). |
toArray(type[] array) | Returns the elements of the array list as an array whose type is the same as the array passed via the parameter. |