java::util::ArrayList Class Reference

Inheritance diagram for java::util::ArrayList:

Inheritance graph
java::util::AbstractListjava::util::AbstractCollectionjava::util::Collectionjava::util::Listjava::lang::Interfacejava::util::RandomAccessjava::lang::Object
[legend]
Collaboration diagram for java::util::ArrayList:

Collaboration graph
java::util::AbstractListjava::util::AbstractCollectionjava::util::Collectionjava::util::Listjava::lang::Interfacejava::util::RandomAccessjava::lang::Comparablejava::lang::Objectjava::lang::ObjectRef
[legend]

List of all members.


Detailed Description

Resizable-array implementation of the List interface.

Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)

The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation.

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. This may reduce the amount of incremental reallocation.

Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list:

	List list = Collections.synchronizedList(new ArrayList(...));
 

The iterators returned by this class's iterator and listIterator methods are fail-fast: if list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. 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 ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

Author:
Josh Bloch
Version:
1.36, 12/03/01
See also:
Collection

List

LinkedList

Vector

Collections::synchronizedList(List)

Since:
1.2

Public Member Functions

 ArrayList (jint initialCapacity=10)
 Constructs an empty list with the specified initial capacity.
 ArrayList (const Collection &c)
 Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
 ArrayList (const Array< ObjectRef > &c)
 Constructs a list containing the elements of the specified Array.
 ArrayList (const ArrayList &src)
virtual ~ArrayList ()
virtual void trimToSize ()
 Trims the capacity of this ArrayList instance to be the list's current size.
virtual void ensureCapacity (jint minCapacity)
 Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
virtual jint size () const
 Returns the number of elements in this list.
virtual jboolean isEmpty () const
 Tests if this list has no elements.
virtual jboolean contains (const ObjectRef &o) const
 Returns true if this list contains the specified element.
virtual jint indexOf (const ObjectRef &o) const
 Searches for the first occurence of the given argument, testing for equality using the equals method.
virtual jint lastIndexOf (const ObjectRef &o) const
 Returns the index of the last occurrence of the specified object in this list.
virtual Array
< ObjectRef
toArray () const
 Returns an array containing all of the elements in this list in the correct order.
virtual ObjectRef get (jint index) const
 Returns the element at the specified position in this list.
virtual ObjectRef set (jint index, const ObjectRef &element)
 Replaces the element at the specified position in this list with the specified element.
virtual jboolean add (const ObjectRef &o)
 Appends the specified element to the end of this list.
virtual void add (jint index, const ObjectRef &element)
 Inserts the specified element at the specified position in this list.
virtual jboolean remove (const ObjectRef &o)
 Removes the first occurrence in this list of the specified element (optional operation).
virtual ObjectRef remove (jint index)
 Removes the element at the specified position in this list.
virtual void swap (jint index1, jint index2)
 Swap the elements at the specified positions in this list.
virtual void clear ()
 Removes all of the elements from this list.
virtual jboolean addAll (const Collection &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.
virtual jboolean addAll (jint index, const Collection &c)
 Inserts all of the elements in the specified collection into this list at the specified position (optional operation).
virtual jboolean equals (const Object &o) const
 Compares the specified object with this set for equality.
virtual jint hashCode () const
 Returns the hash code value for this set.
virtual String toString () const
 Returns a string representation of this set.

Protected Member Functions

virtual void removeRange (jint fromIndex, jint toIndex)
 Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.

Constructor & Destructor Documentation

java::util::ArrayList::ArrayList ( jint  initialCapacity = 10  ) 

Constructs an empty list with the specified initial capacity.

Parameters:
initialCapacity the initial capacity of the list.
Exceptions:
IllegalArgumentException if the specified initial capacity is negative

java::util::ArrayList::ArrayList ( const Collection c  ) 

Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

The ArrayList instance has an initial capacity of 110% the size of the specified collection.

Parameters:
c the collection whose elements are to be placed into this list.
Exceptions:
NullPointerException if the specified collection is null.

java::util::ArrayList::ArrayList ( const Array< ObjectRef > &  c  ) 

Constructs a list containing the elements of the specified Array.

Parameters:
c the Array whose elements are to be referenced into this list.
Exceptions:
NullPointerException if the specified array is null.

java::util::ArrayList::ArrayList ( const ArrayList src  ) 

virtual java::util::ArrayList::~ArrayList (  )  [virtual]


Member Function Documentation

virtual void java::util::ArrayList::trimToSize (  )  [virtual]

Trims the capacity of this ArrayList instance to be the list's current size.

An application can use this operation to minimize the storage of an ArrayList instance.

virtual void java::util::ArrayList::ensureCapacity ( jint  minCapacity  )  [virtual]

Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.

Parameters:
minCapacity the desired minimum capacity.

virtual jint java::util::ArrayList::size (  )  const [virtual]

Returns the number of elements in this list.

Returns:
the number of elements in this list.

Implements java::util::Collection.

virtual jboolean java::util::ArrayList::isEmpty (  )  const [virtual]

Tests if this list has no elements.

Returns:
true if this list has no elements; false otherwise.

Reimplemented from java::util::AbstractList.

virtual jboolean java::util::ArrayList::contains ( const ObjectRef o  )  const [virtual]

Returns true if this list contains the specified element.

Parameters:
elem element whose presence in this List is to be tested.
Returns:
true if the specified element is present; false otherwise.

Reimplemented from java::util::AbstractList.

virtual jint java::util::ArrayList::indexOf ( const ObjectRef o  )  const [virtual]

Searches for the first occurence of the given argument, testing for equality using the equals method.

Parameters:
elem an object.
Returns:
the index of the first occurrence of the argument in this list; returns -1 if the object is not found.
See also:
Object::equals(Object)

Reimplemented from java::util::AbstractList.

virtual jint java::util::ArrayList::lastIndexOf ( const ObjectRef o  )  const [virtual]

Returns the index of the last occurrence of the specified object in this list.

Parameters:
elem the desired element.
Returns:
the index of the last occurrence of the specified object in this list; returns -1 if the object is not found.

Reimplemented from java::util::AbstractList.

virtual Array<ObjectRef> java::util::ArrayList::toArray (  )  const [virtual]

Returns an array containing all of the elements in this list in the correct order.

Returns:
an array containing all of the elements in this list in the correct order.

Reimplemented from java::util::AbstractList.

virtual ObjectRef java::util::ArrayList::get ( jint  index  )  const [virtual]

Returns the element at the specified position in this list.

Parameters:
index index of element to return.
Returns:
the element at the specified position in this list.
Exceptions:
IndexOutOfBoundsException if index is out of range (index < 0 || index >= size()).

Implements java::util::AbstractList.

virtual ObjectRef java::util::ArrayList::set ( jint  index,
const ObjectRef element 
) [virtual]

Replaces the element at the specified position in this list with the specified element.

Parameters:
index index of element to replace.
element element to be stored at the specified position.
Returns:
the element previously at the specified position.
Exceptions:
IndexOutOfBoundsException if index out of range (index < 0 || index >= size()).

Reimplemented from java::util::AbstractList.

virtual jboolean java::util::ArrayList::add ( const ObjectRef o  )  [virtual]

Appends the specified element to the end of this list.

Parameters:
o element to be appended to this list.
Returns:
true (as per the general contract of Collection.add).

Reimplemented from java::util::AbstractList.

virtual void java::util::ArrayList::add ( jint  index,
const ObjectRef element 
) [virtual]

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).

Parameters:
index index at which the specified element is to be inserted.
element element to be inserted.
Exceptions:
IndexOutOfBoundsException if index is out of range (index < 0 || index > size()).

Reimplemented from java::util::AbstractList.

virtual jboolean java::util::ArrayList::remove ( const ObjectRef o  )  [virtual]

Removes the first occurrence in this list of the specified element (optional operation).

If this 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).

Parameters:
o element to be removed from this list, if present.
Returns:
true if this list contained the specified element.
Exceptions:
ClassCastException if the type of the specified element is incompatible with this list (optional).
NullPointerException if the specified element is null and this list does not support null elements (optional).
UnsupportedOperationException if the remove method is not supported by this list.

Reimplemented from java::util::AbstractList.

virtual ObjectRef java::util::ArrayList::remove ( jint  index  )  [virtual]

Removes the element at the specified position in this list.

Shifts any subsequent elements to the left (subtracts one from their indices).

Parameters:
index the index of the element to removed.
Returns:
the element that was removed from the list.
Exceptions:
IndexOutOfBoundsException if index out of range (index < 0 || index >= size()).

Reimplemented from java::util::AbstractList.

virtual void java::util::ArrayList::swap ( jint  index1,
jint  index2 
) [virtual]

Swap the elements at the specified positions in this list.

Parameters:
index1 the index of the first element to swap.
index2 the index of the second element to swap.
Exceptions:
IndexOutOfBoundsException if index out of range (index < 0 || index >= size()).

virtual void java::util::ArrayList::clear (  )  [virtual]

Removes all of the elements from this list.

The list will be empty after this call returns.

Reimplemented from java::util::AbstractList.

virtual jboolean java::util::ArrayList::addAll ( const Collection c  )  [virtual]

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.)

Parameters:
c the elements to be inserted into this list.
Returns:
true if this list changed as a result of the call.
Exceptions:
IndexOutOfBoundsException if index out of range (index < 0 || index > size()).
NullPointerException if the specified collection is null.

Reimplemented from java::util::AbstractList.

virtual jboolean java::util::ArrayList::addAll ( jint  index,
const Collection c 
) [virtual]

Inserts all of the elements in the specified collection into this list at the specified position (optional operation).

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 this list in the order that they are returned by the specified collection's iterator. The behavior of this operation is unspecified if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

Parameters:
index index at which to insert first element from the specified collection.
c elements to be inserted into this list.
Returns:
true if this list changed as a result of the call.
Exceptions:
UnsupportedOperationException if the addAll method is not supported by this list.
ClassCastException if the class of one of elements of the specified collection prevents it from being added to this list.
NullPointerException if the specified collection contains one or more null elements and this list does not support null elements, or if the specified collection is null.
IllegalArgumentException if some aspect of one of elements of the specified collection prevents it from being added to this list.
IndexOutOfBoundsException if the index is out of range (index < 0 || index > size()).

Reimplemented from java::util::AbstractList.

virtual jboolean java::util::ArrayList::equals ( const Object o  )  const [virtual]

Compares the specified object with this set for equality.

Returns true if the given object is also a set, the two sets have the same size, and every member of the given set is contained in this set. This ensures that the equals method works properly across different implementations of the Set interface.

This implementation first checks if the specified object is this set; if so it returns true. Then, it checks if the specified object is a set whose size is identical to the size of this set; if not, it it returns false. If so, it returns containsAll((Collection) o).

Parameters:
o Object to be compared for equality with this set.
Returns:
true if the specified object is equal to this set.

Reimplemented from java::util::AbstractList.

virtual jint java::util::ArrayList::hashCode (  )  const [virtual]

Returns the hash code value for this set.

The hash code of a set is defined to be the sum of the hash codes of the elements in the set. This ensures that s1.equals(s2) implies that s1.hashCode()==s2.hashCode() for any two sets s1 and s2, as required by the general contract of Object.hashCode.

This implementation enumerates over the set, calling the hashCode method on each element in the collection, and adding up the results.

Returns:
the hash code value for this set.

Reimplemented from java::util::AbstractList.

virtual String java::util::ArrayList::toString (  )  const [virtual]

Returns a string representation of this set.

The string representation consists of a list of the set's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).

This implementation creates an empty string buffer, appends a left square bracket, and iterates over the collection appending the string representation of each element in turn. After appending each element except the last, the string ", " is appended. Finally a right bracket is appended. A string is obtained from the string buffer, and returned.

Returns:
a string representation of this set.

Reimplemented from java::util::AbstractList.

virtual void java::util::ArrayList::removeRange ( jint  fromIndex,
jint  toIndex 
) [protected, virtual]

Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.

Shifts any succeeding elements to the left (reduces their index). This call shortens the ArrayList by (toIndex - fromIndex) elements. (If toIndex==fromIndex, this operation has no effect.)

This method is called by the clear operation on this list and its subLists. Overriding this method to take advantage of the internals of the list implementation can substantially improve the performance of the clear operation on this list and its subLists.

This implementation gets a list iterator positioned before fromIndex, and repeatedly calls ListIterator.next followed by ListIterator.remove until the entire range has been removed. Note: if ListIterator.remove requires linear time, this implementation requires quadratic time.

Parameters:
fromIndex index of first element to be removed.
toIndex index after last element to be removed.

Reimplemented from java::util::AbstractList.


The documentation for this class was generated from the following file:
Generated on Fri May 16 11:56:46 2008 for CrossPlatformJavaLikeC++API by  doxygen 1.5.3