Package java.util
Class ArrayList<E>
- java.lang.Object
-
- java.util.ArrayList<E>
-
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,List<E>
public class ArrayList<E> extends Object implements List<E>
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(int index, E element)
Inserts the specified element at the specified position in this list.boolean
add(E e)
Appends the specified element to the end of this list.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.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.void
clear()
Removes all of the elements from this list.boolean
contains(Object o)
Returnstrue
if this list contains the specified element.boolean
containsAll(Collection<?> arg0)
Tests whether thisList
contains all objects contained in the specified collection.(package private) E
elementData(int index)
E
get(int index)
Returns the element at the specified position in this list.int
indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.(package private) int
indexOfRange(Object o, int start, int end)
boolean
isEmpty()
Returnstrue
if this list contains no elements.Iterator<E>
iterator()
Returns an iterator over the elements in this list in proper sequence.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.(package private) int
lastIndexOfRange(Object o, int start, int end)
ListIterator<E>
listIterator()
Returns a list iterator over the elements in this list (in proper sequence).ListIterator<E>
listIterator(int index)
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.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.boolean
removeAll(Collection<?> c)
Removes from this list all of its elements that are contained in the specified collection.boolean
retainAll(Collection<?> c)
Retains only the elements in this list that are contained in the specified collection.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.List<E>
subList(int arg0, int arg1)
Returns aList
of the specified portion of thisList
from the given start index to the end index minus one.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[] arg0)
Returns an array containing all elements contained in thisList
.
-
-
-
Constructor Detail
-
ArrayList
public ArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.- Parameters:
initialCapacity
- the initial capacity of the list- Throws:
IllegalArgumentException
- if the specified initial capacity is negative
-
ArrayList
public ArrayList()
Constructs an empty list with an initial capacity of ten.
-
-
Method Detail
-
size
public int size()
Returns the number of elements in this list.
-
isEmpty
public boolean isEmpty()
Returnstrue
if this list contains no elements.- Specified by:
isEmpty
in interfaceCollection<E>
- Specified by:
isEmpty
in interfaceList<E>
- Returns:
true
if this list contains no elements- See Also:
List.size()
-
contains
public boolean contains(Object o)
Returnstrue
if this list contains the specified element. More formally, returnstrue
if and only if this list contains at least one elemente
such thatObjects.equals(o, e)
.
-
indexOf
public int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest indexi
such thatObjects.equals(o, get(i))
, or -1 if there is no such index.
-
indexOfRange
int indexOfRange(Object o, int start, int end)
-
lastIndexOf
public 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. More formally, returns the highest indexi
such thatObjects.equals(o, get(i))
, or -1 if there is no such index.- Specified by:
lastIndexOf
in interfaceList<E>
- Parameters:
o
- the object to search for.- Returns:
- the index of the last occurrence of the object, or -1 if the object was not found.
-
lastIndexOfRange
int lastIndexOfRange(Object o, int start, int end)
-
get
public E get(int index)
Returns the element at the specified position in this list.- Specified by:
get
in interfaceList<E>
- Parameters:
index
- index of the element to return- Returns:
- the element at the specified position in this list
- Throws:
IndexOutOfBoundsException
- iflocation < 0 || location >= size()
-
set
public E set(int index, E element)
Replaces the element at the specified position in this list with the specified element.- Specified by:
set
in interfaceList<E>
- Parameters:
index
- index of the element to replaceelement
- element to be stored at the specified position- Returns:
- the element previously at the specified position
- Throws:
IndexOutOfBoundsException
- iflocation < 0 || location >= size()
-
add
public boolean add(E e)
Appends the specified element to the end of this list.- Specified by:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceList<E>
- Parameters:
e
- element to be appended to this list- Returns:
true
(as specified byCollection.add(E)
)
-
add
public void add(int index, E element)
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).- Specified by:
add
in interfaceList<E>
- Parameters:
index
- index at which the specified element is to be insertedelement
- element to be inserted- Throws:
IndexOutOfBoundsException
- iflocation < 0 || location > size()
-
addAll
public 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. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified collection is this list, and this list is nonempty.)- Specified by:
addAll
in interfaceCollection<E>
- Specified by:
addAll
in interfaceList<E>
- Parameters:
c
- collection containing elements to be added to this list- Returns:
- true if this list changed as a result of the call
- Throws:
NullPointerException
- if the specified collection is null
-
addAll
public 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. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that they are returned by the specified collection's iterator.- Specified by:
addAll
in interfaceList<E>
- Parameters:
index
- index at which to insert the first element from the specified collectionc
- collection containing elements to be added to this list- Returns:
- true if this list changed as a result of the call
- Throws:
IndexOutOfBoundsException
- iflocation < 0 || location > size()
NullPointerException
- if the specified collection is null
-
clear
public void clear()
Removes all of the elements from this list. The list will be empty after this call returns.- Specified by:
clear
in interfaceCollection<E>
- Specified by:
clear
in interfaceList<E>
- See Also:
List.isEmpty()
,List.size()
-
containsAll
public boolean containsAll(Collection<?> arg0)
Description copied from interface:List
Tests whether thisList
contains all objects contained in the specified collection.- Specified by:
containsAll
in interfaceCollection<E>
- Specified by:
containsAll
in interfaceList<E>
- Parameters:
arg0
- the collection of objects- Returns:
true
if all objects in the specified collection are elements of thisList
,false
otherwise.
-
remove
public E remove(int index)
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).- Specified by:
remove
in interfaceList<E>
- Parameters:
index
- the index of the element to be removed- Returns:
- the element that was removed from the list
- Throws:
IndexOutOfBoundsException
- iflocation < 0 || location >= size()
-
remove
public boolean remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).
-
removeAll
public boolean removeAll(Collection<?> c)
Removes from this list all of its elements that are contained in the specified collection.- Specified by:
removeAll
in interfaceCollection<E>
- Specified by:
removeAll
in interfaceList<E>
- Parameters:
c
- collection containing elements to be removed from this list- Returns:
true
if this list changed as a result of the call- Throws:
ClassCastException
- if the class of an element of this list is incompatible with the specified collection (optional)NullPointerException
- if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null- See Also:
Collection.contains(Object)
-
retainAll
public boolean retainAll(Collection<?> c)
Retains only the elements in this list that are contained in the specified collection. In other words, removes from this list all of its elements that are not contained in the specified collection.- Specified by:
retainAll
in interfaceCollection<E>
- Specified by:
retainAll
in interfaceList<E>
- Parameters:
c
- collection containing elements to be retained in this list- Returns:
true
if this list changed as a result of the call- Throws:
ClassCastException
- if the class of an element of this list is incompatible with the specified collection (optional)NullPointerException
- if this list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null- See Also:
Collection.contains(Object)
-
toArray
public Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
-
toArray
public <T> T[] toArray(T[] arg0)
Description copied from interface:List
Returns an array containing all elements contained in thisList
. If the specified array is large enough to hold the elements, the specified array is used, otherwise an array of the same type is created. If the specified array is used and is larger than thisList
, the array element following the collection elements is set to null.
-
subList
public List<E> subList(int arg0, int arg1)
Description copied from interface:List
Returns aList
of the specified portion of thisList
from the given start index to the end index minus one. The returnedList
is backed by thisList
so changes to it are reflected by the other.
-
iterator
public Iterator<E> iterator()
Returns an iterator over the elements in this list in proper sequence.
-
listIterator
public ListIterator<E> listIterator()
Returns a list iterator over the elements in this list (in proper sequence).The returned list iterator is fail-fast.
- Specified by:
listIterator
in interfaceList<E>
- Returns:
- a
List
iterator on the elements of thisList
- See Also:
listIterator(int)
-
listIterator
public ListIterator<E> listIterator(int index)
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. The specified index indicates the first element that would be returned by an initial call tonext
. An initial call toprevious
would return the element with the specified index minus one.The returned list iterator is fail-fast.
- Specified by:
listIterator
in interfaceList<E>
- Parameters:
index
- the index at which to start the iteration.- Returns:
- a list iterator on the elements of this
List
. - Throws:
IndexOutOfBoundsException
- iflocation < 0 || location > size()
- See Also:
ListIterator
-
elementData
E elementData(int index)
-
-