Documente Academic
Documente Profesional
Documente Cultură
Package
Class
Use Frames
Tree
Deprecated
Next Class
No Frames
java.util
Class ArrayList<E>
java.lang.Object java.util.AbstractCollection<E> java.util.AbstractList<E> java.util.ArrayList<E>
L i s tl i s t=C o l l e c t i o n s . s y n c h r o n i z e d L i s t ( n e wA r r a y L i s t ( . . . ) ) ;
The iterators returned by this class's i t e r a t o rand l i s t I t e r a t o rmethods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own r e m o v eor a d d methods, the iterator will throw a C o n c u r r e n t M o d i f i c a t i o n E x c e p t i o n . Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw C o n c u r r e n t M o d i f i c a t i o n E x c e p t i o non a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast b ehavior of iterators should b e used only to detect b ugs.
docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html 1/16
10/30/13
Since:
1.2
See Also:
C o l l e c t i o n ,L i s t ,L i n k e d L i s t ,V e c t o r , Serialized Form
Constructor Summary
Constructors Constructor and Description A r r a y L i s t ( ) Constructs an empty list with an initial capacity of ten. A r r a y L i s t ( C o l l e c t i o n < ?e x t e n d sE >c ) Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. A r r a y L i s t ( i n ti n i t i a l C a p a c i t y ) Constructs an empty list with the specified initial capacity.
Method Summary
Methods Modifier and Type b o o l e a n v o i d b o o l e a n Method and Description a d d ( Ee ) Appends the specified element to the end of this list. a d d ( i n ti n d e x ,Ee l e m e n t ) Inserts the specified element at the specified position in this list. a d d A l l ( C o l l e c t i o n < ?e x t e n d sE >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. b o o l e a n a d d A l l ( i n ti n d e x ,C o l l e c t i o n < ?e x t e n d sE >c ) Inserts all of the elements in the specified collection into this list, starting at the specified position. v o i d O b j e c t c l e a r ( ) Removes all of the elements from this list. c l o n e ( ) Returns a shallow copy of this A r r a y L i s tinstance.
docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
2/16
10/30/13
b o o l e a n v o i d
c o n t a i n s ( O b j e c to ) Returns t r u eif this list contains the specified element. e n s u r e C a p a c i t y ( i n tm i n C a p a c i t y ) Increases the capacity of this A r r a y L i s tinstance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
E i n t
g e t ( i n ti n d e x ) Returns the element at the specified position in this list. i n d e x O f ( O b j e c to ) Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
b o o l e a n I t e r a t o r < E > i n t
i s E m p t y ( ) Returns t r u eif this list contains no elements. i t e r a t o r ( ) Returns an iterator over the elements in this list in proper sequence. l a s t I n d e x O f ( O b j e c to ) Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
l i s t I t e r a t o r ( ) Returns a list iterator over the elements in this list (in proper sequence). l i s t I t e r a t o r ( i n ti n d e x ) Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
E b o o l e a n
r e m o v e ( i n ti n d e x ) Removes the element at the specified position in this list. r e m o v e ( O b j e c to ) Removes the first occurrence of the specified element from this list, if it is present.
b o o l e a n
r e m o v e A l l ( C o l l e c t i o n < ? >c ) Removes from this list all of its elements that are contained in the specified collection.
p r o t e c t e dv o i d
r e m o v e R a n g e ( i n tf r o m I n d e x ,i n tt o I n d e x ) Removes from this list all of the elements whose index is between f r o m I n d e x , inclusive, and t o I n d e x , exclusive.
b o o l e a n
r e t a i n A l l ( C o l l e c t i o n < ? >c ) Retains only the elements in this list that are contained in the specified collection.
s e t ( i n ti n d e x ,Ee l e m e n t ) Replaces the element at the specified position in this list with the specified element.
i n t L i s t < E >
s i z e ( ) Returns the number of elements in this list. s u b L i s t ( i n tf r o m I n d e x ,i n tt o I n d e x ) Returns a view of the portion of this list between the specified f r o m I n d e x , inclusive, and t o I n d e x , exclusive.
O b j e c t [ ]
t o A r r a y ( ) Returns an array containing all of the elements in this list in proper sequence (from first to last element).
< T >T [ ]
t o A r r a y ( 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.
docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
3/16
10/30/13
v o i d
Constructor Detail
ArrayList
p u b l i cA r r a y L i s t ( i n ti n i t i a l C a p a c i t y )
Constructs an empty list with the specified initial capacity.
Parameters:
i n i t i a l C a p a c i t y- the initial capacity of the list
Throws:
I l l e g a l A r g u m e n t E x c e p t i o n- if the specified initial capacity is negative
ArrayList
p u b l i cA r r a y L i s t ( )
Constructs an empty list with an initial capacity of ten.
ArrayList
p u b l i cA r r a y L i s t ( C o l l e c t i o n < ?e x t e n d sE >c )
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
Parameters:
c- the collection whose elements are to be placed into this list
Throws:
docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html 4/16
10/30/13
Method Detail
trimToSize
p u b l i cv o i dt r i m T o S i z e ( )
Trims the capacity of this A r r a y L i s tinstance to be the list's current size. An application can use this operation to minimize the storage of an A r r a y L i s tinstance.
ensureCapacity
p u b l i cv o i de n s u r e C a p a c i t y ( i n tm i n C a p a c i t y )
Increases the capacity of this A r r a y L i s tinstance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
Parameters:
m i n C a p a c i t y- the desired minimum capacity
size
p u b l i ci n ts i z e ( )
Returns the number of elements in this list.
Specified by:
s i z ein interface C o l l e c t i o n < E >
Specified by:
s i z ein interface L i s t < E >
Specified by:
s i z ein class A b s t r a c t C o l l e c t i o n < E >
Returns:
the number of elements in this list
isEmpty
p u b l i cb o o l e a ni s E m p t y ( )
Returns t r u eif this list contains no elements.
Specified by:
i s E m p t yin interface C o l l e c t i o n < E >
Specified by:
docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html 5/16
10/30/13
Overrides:
i s E m p t yin class A b s t r a c t C o l l e c t i o n < E >
Returns:
t r u eif this list contains no elements
contains
p u b l i cb o o l e a nc o n t a i n s ( O b j e c to )
Returns t r u eif this list contains the specified element. More formally, returns t r u eif and only if this list contains at least one element esuch that ( o = = n u l l?e = = n u l l:o . e q u a l s ( e ) ) .
Specified by:
c o n t a i n sin interface C o l l e c t i o n < E >
Specified by:
c o n t a i n sin interface L i s t < E >
Overrides:
c o n t a i n sin class A b s t r a c t C o l l e c t i o n < E >
Parameters:
o- element whose presence in this list is to be tested
Returns:
t r u eif this list contains the specified element
indexOf
p u b l i ci n ti n d e x O f ( O b j e c to )
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 index isuch that ( o = = n u l l?g e t ( i ) = = n u l l:o . e q u a l s ( g e t ( i ) ) ) , or -1 if there is no such index.
Specified by:
i n d e x O fin interface L i s t < E >
Overrides:
i n d e x O fin class A b s t r a c t L i s t < E >
Parameters:
o- element to search for
Returns:
the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element
lastIndexOf
docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html 6/16
10/30/13
p u b l i ci n tl a s t I n d e x O f ( O b j e c to )
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 index isuch that ( o = = n u l l?g e t ( i ) = = n u l l:o . e q u a l s ( g e t ( i ) ) ) , or -1 if there is no such index.
Specified by:
l a s t I n d e x O fin interface L i s t < E >
Overrides:
l a s t I n d e x O fin class A b s t r a c t L i s t < E >
Parameters:
o- element to search for
Returns:
the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element
clone
p u b l i cO b j e c tc l o n e ( )
Returns a shallow copy of this A r r a y L i s tinstance. (The elements themselves are not copied.)
Overrides:
c l o n ein class O b j e c t
Returns:
a clone of this A r r a y L i s tinstance
See Also:
C l o n e a b l e
toArray
p u b l i cO b j e c t [ ]t o A r r a y ( )
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.
Specified by:
t o A r r a yin interface C o l l e c t i o n < E >
Specified by:
t o A r r a yin interface L i s t < E >
Overrides:
t o A r r a yin class A b s t r a c t C o l l e c t i o n < E >
Returns:
an array containing all of the elements in this list in proper sequence
docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html 7/16
10/30/13
See Also:
A r r a y s . a s L i s t ( O b j e c t [ ] )
toArray
p u b l i c< T >T [ ]t o A r r a y ( 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. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list. If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to n u l l . (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)
Specified by:
t o A r r a yin interface C o l l e c t i o n < E >
Specified by:
t o A r r a yin interface L i s t < E >
Overrides:
t o A r r a yin class A b s t r a c t C o l l e c t i o n < E >
Parameters:
a- the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
an array containing the elements of the list
Throws:
A r r a y S t o r e E x c e p t i o n- if the runtime type of the specified array is not a supertype of the runtime type of every element in this list N u l l P o i n t e r E x c e p t i o n- if the specified array is null
get
p u b l i cEg e t ( i n ti n d e x )
Returns the element at the specified position in this list.
Specified by:
g e tin interface L i s t < E >
Specified by:
g e tin class A b s t r a c t L i s t < E >
Parameters:
i n d e x- index of the element to return
Returns:
the element at the specified position in this list
docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html 8/16
10/30/13
Throws:
I n d e x O u t O f B o u n d s E x c e p t i o n- if the index is out of range (i n d e x<0| |i n d e x> =s i z e ( ) )
set
p u b l i cEs e t ( i n ti n d e x , Ee l e m e n t )
Replaces the element at the specified position in this list with the specified element.
Specified by:
s e tin interface L i s t < E >
Overrides:
s e tin class A b s t r a c t L i s t < E >
Parameters:
i n d e x- index of the element to replace e l e m e n t- element to be stored at the specified position
Returns:
the element previously at the specified position
Throws:
I n d e x O u t O f B o u n d s E x c e p t i o n- if the index is out of range (i n d e x<0| |i n d e x> =s i z e ( ) )
add
p u b l i cb o o l e a na d d ( Ee )
Appends the specified element to the end of this list.
Specified by:
a d din interface C o l l e c t i o n < E >
Specified by:
a d din interface L i s t < E >
Overrides:
a d din class A b s t r a c t L i s t < E >
Parameters:
e- element to be appended to this list
Returns:
t r u e(as specified by C o l l e c t i o n . a d d ( E ) )
add
p u b l i cv o i da d d ( i n ti n d e x , Ee l e m e n t )
docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html 9/16
10/30/13
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:
a d din interface L i s t < E >
Overrides:
a d din class A b s t r a c t L i s t < E >
Parameters:
i n d e x- index at which the specified element is to be inserted e l e m e n t- element to be inserted
Throws:
I n d e x O u t O f B o u n d s E x c e p t i o n- if the index is out of range (i n d e x<0| |i n d e x>s i z e ( ) )
remove
p u b l i cEr e m o v e ( i n ti n d e x )
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).
Specified by:
r e m o v ein interface L i s t < E >
Overrides:
r e m o v ein class A b s t r a c t L i s t < E >
Parameters:
i n d e x- the index of the element to be removed
Returns:
the element that was removed from the list
Throws:
I n d e x O u t O f B o u n d s E x c e p t i o n- if the index is out of range (i n d e x<0| |i n d e x> =s i z e ( ) )
remove
p u b l i cb o o l e a nr e m o v e ( O b j e c to )
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 isuch that ( o = = n u l l?g e t ( i ) = = n u l l:o . e q u a l s ( g e t ( i ) ) )(if such an element exists). Returns t r u eif this list contained the specified element (or equivalently, if this list changed as a result of the call).
Specified by:
r e m o v ein interface C o l l e c t i o n < E >
Specified by:
r e m o v ein interface L i s t < E >
Overrides:
docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html 10/16
10/30/13
Parameters:
o- element to be removed from this list, if present
Returns:
t r u eif this list contained the specified element
clear
p u b l i cv o i dc l e a r ( )
Removes all of the elements from this list. The list will be empty after this call returns.
Specified by:
c l e a rin interface C o l l e c t i o n < E >
Specified by:
c l e a rin interface L i s t < E >
Overrides:
c l e a rin class A b s t r a c t L i s t < E >
addAll
p u b l i cb o o l e a na d d A l l ( C o l l e c t i o n < ?e x t e n d sE >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:
a d d A l lin interface C o l l e c t i o n < E >
Specified by:
a d d A l lin interface L i s t < E >
Overrides:
a d d A l lin class A b s t r a c t C o l l e c t i o n < E >
Parameters:
c- collection containing elements to be added to this list
Returns:
t r u eif this list changed as a result of the call
Throws:
N u l l P o i n t e r E x c e p t i o n- if the specified collection is null
See Also:
A b s t r a c t C o l l e c t i o n . a d d ( O b j e c t )
docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
11/16
10/30/13
addAll
p u b l i cb o o l e a na d d A l l ( i n ti n d e x , C o l l e c t i o n < ?e x t e n d sE >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:
a d d A l lin interface L i s t < E >
Overrides:
a d d A l lin class A b s t r a c t L i s t < E >
Parameters:
i n d e x- index at which to insert the first element from the specified collection c- collection containing elements to be added to this list
Returns:
t r u eif this list changed as a result of the call
Throws:
I n d e x O u t O f B o u n d s E x c e p t i o n- if the index is out of range (i n d e x<0| |i n d e x>s i z e ( ) ) N u l l P o i n t e r E x c e p t i o n- if the specified collection is null
removeRange
p r o t e c t e dv o i dr e m o v e R a n g e ( i n tf r o m I n d e x , i n tt o I n d e x )
Removes from this list all of the elements whose index is between f r o m I n d e x , inclusive, and t o I n d e x , exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by ( t o I n d e x-f r o m I n d e x )elements. (If t o I n d e x = = f r o m I n d e x , this operation has no effect.)
Overrides:
r e m o v e R a n g ein class A b s t r a c t L i s t < E >
Parameters:
f r o m I n d e x- index of first element to be removed t o I n d e x- index after last element to be removed
Throws:
I n d e x O u t O f B o u n d s E x c e p t i o n- if f r o m I n d e xor t o I n d e xis out of range (f r o m I n d e x<0| | f r o m I n d e x> =s i z e ( )| |t o I n d e x>s i z e ( )| |t o I n d e x<f r o m I n d e x )
removeAll
p u b l i cb o o l e a nr e m o v e A l l ( C o l l e c t i o n < ? >c )
Removes from this list all of its elements that are contained in the specified collection.
Specified by:
docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html 12/16
10/30/13
Specified by:
r e m o v e A l lin interface L i s t < E >
Overrides:
r e m o v e A l lin class A b s t r a c t C o l l e c t i o n < E >
Parameters:
c- collection containing elements to be removed from this list
Returns:
t r u eif this list changed as a result of the call
Throws:
C l a s s C a s t E x c e p t i o n- if the class of an element of this list is incompatible with the specified collection (optional) N u l l P o i n t e r E x c e p t i o n- 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:
C o l l e c t i o n . c o n t a i n s ( O b j e c t )
retainAll
p u b l i cb o o l e a nr e t a i n A l l ( C o l l e c t i o n < ? >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:
r e t a i n A l lin interface C o l l e c t i o n < E >
Specified by:
r e t a i n A l lin interface L i s t < E >
Overrides:
r e t a i n A l lin class A b s t r a c t C o l l e c t i o n < E >
Parameters:
c- collection containing elements to be retained in this list
Returns:
t r u eif this list changed as a result of the call
Throws:
C l a s s C a s t E x c e p t i o n- if the class of an element of this list is incompatible with the specified collection (optional) N u l l P o i n t e r E x c e p t i o n- 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:
C o l l e c t i o n . c o n t a i n s ( O b j e c t )
docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
13/16
10/30/13
listIterator
p u b l i cL i s t I t e r a t o r < E >l i s t I t e r a t o r ( i n ti n d e x )
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 to n e x t . An initial call to p r e v i o u swould return the element with the specified index minus one. The returned list iterator is fail-fast.
Specified by:
l i s t I t e r a t o rin interface L i s t < E >
Overrides:
l i s t I t e r a t o rin class A b s t r a c t L i s t < E >
Parameters:
i n d e x- index of the first element to be returned from the list iterator (by a call to n e x t )
Returns:
a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list
Throws:
I n d e x O u t O f B o u n d s E x c e p t i o n- if the index is out of range (i n d e x<0| |i n d e x>s i z e ( ) )
listIterator
p u b l i cL i s t I t e r a t o r < E >l i s t I t e r a t o r ( )
Returns a list iterator over the elements in this list (in proper sequence). The returned list iterator is fail-fast.
Specified by:
l i s t I t e r a t o rin interface L i s t < E >
Overrides:
l i s t I t e r a t o rin class A b s t r a c t L i s t < E >
Returns:
a list iterator over the elements in this list (in proper sequence)
See Also:
l i s t I t e r a t o r ( i n t )
iterator
p u b l i cI t e r a t o r < E >i t e r a t o r ( )
Returns an iterator over the elements in this list in proper sequence. The returned iterator is fail-fast.
Specified by:
i t e r a t o rin interface I t e r a b l e < E >
docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html 14/16
10/30/13
Specified by:
i t e r a t o rin interface C o l l e c t i o n < E >
Specified by:
i t e r a t o rin interface L i s t < E >
Overrides:
i t e r a t o rin class A b s t r a c t L i s t < E >
Returns:
an iterator over the elements in this list in proper sequence
subList
p u b l i cL i s t < E >s u b L i s t ( i n tf r o m I n d e x , i n tt o I n d e x )
Returns a view of the portion of this list between the specified f r o m I n d e x , inclusive, and t o I n d e x , exclusive. (If f r o m I n d e xand t o I n d e xare equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations. This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:
l i s t . s u b L i s t ( f r o m ,t o ) . c l e a r ( ) ;
Similar idioms may be constructed for i n d e x O f ( O b j e c t )and l a s t I n d e x O f ( O b j e c t ) , and all of the algorithms in the C o l l e c t i o n sclass can be applied to a subList. The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)
Specified by:
s u b L i s tin interface L i s t < E >
Overrides:
s u b L i s tin class A b s t r a c t L i s t < E >
Parameters:
f r o m I n d e x- low endpoint (inclusive) of the subList t o I n d e x- high endpoint (exclusive) of the subList
Returns:
a view of the specified range within this list
Throws:
I n d e x O u t O f B o u n d s E x c e p t i o n- if an endpoint index value is out of range ( f r o m I n d e x<0| | t o I n d e x>s i z e ) I l l e g a l A r g u m e n t E x c e p t i o n- if the endpoint indices are out of order ( f r o m I n d e x>t o I n d e x )
docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
15/16
10/30/13
Package
Class
Use Frames
Tree
Deprecated
Next Class
No Frames
Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples. Copyright 1993, 2013, Oracle and/or its affiliates. All rights reserved.
docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
16/16