Build 1.0_r1(from source)

java.util
Class ArrayList<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.ArrayList<E>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess

public class ArrayList<E>
extends AbstractList<E>
implements List<E>, Cloneable, Serializable, RandomAccess

ArrayList is an implementation of List, backed by an array. All optional operations are supported, adding, removing, and replacing. The elements can be any objects.

Since:
1.2
See Also:
Serialized Form

Field Summary
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
ArrayList()
          Constructs a new instance of ArrayList with zero capacity.
ArrayList(Collection<? extends E> collection)
          Constructs a new instance of ArrayList containing the elements in the specified collection.
ArrayList(int capacity)
          Constructs a new instance of ArrayList with the specified capacity.
 
Method Summary
 boolean add(E object)
          Adds the specified object at the end of this ArrayList.
 void add(int location, E object)
          Inserts the specified object into this ArrayList at the specified location.
 boolean addAll(Collection<? extends E> collection)
          Adds the objects in the specified Collection to this ArrayList.
 boolean addAll(int location, Collection<? extends E> collection)
          Inserts the objects in the specified Collection at the specified location in this ArrayList.
 void clear()
          Removes all elements from this ArrayList, leaving it empty.
 Object clone()
          Returns a new ArrayList with the same elements, size and capacity as this ArrayList.
 boolean contains(Object object)
          Searches this ArrayList for the specified object.
 void ensureCapacity(int minimumCapacity)
          Ensures that this ArrayList can hold the specified number of elements without growing.
 E get(int location)
          Returns the element at the specified location in this ArrayList.
 int indexOf(Object object)
          Searches this ArrayList for the specified object and returns the index of the first occurrence.
 boolean isEmpty()
          Returns if this ArrayList has no elements, a size of zero.
 int lastIndexOf(Object object)
          Searches this ArrayList for the specified object and returns the index of the last occurrence.
 E remove(int location)
          Removes the object at the specified location from this ArrayList.
 boolean remove(Object object)
          Removes the first occurrence of the specified object from this Collection.
protected  void removeRange(int start, int end)
          Removes the objects in the specified range from the start to the end, but not including the end index.
 E set(int location, E object)
          Replaces the element at the specified location in this ArrayList with the specified object.
 int size()
          Returns the number of elements in this ArrayList.
 Object[] toArray()
          Returns a new array containing all elements contained in this ArrayList.
<T> T[]
toArray(T[] contents)
          Returns an array containing all elements contained in this ArrayList.
 void trimToSize()
          Sets the capacity of this ArrayList to be the same as the size.
 
Methods inherited from class java.util.AbstractList
equals, hashCode, iterator, listIterator, listIterator, subList
 
Methods inherited from class java.util.AbstractCollection
containsAll, removeAll, retainAll, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
containsAll, equals, hashCode, iterator, listIterator, listIterator, removeAll, retainAll, subList
 

Constructor Detail

ArrayList

public ArrayList()
Constructs a new instance of ArrayList with zero capacity.


ArrayList

public ArrayList(int capacity)
Constructs a new instance of ArrayList with the specified capacity.

Parameters:
capacity - the initial capacity of this ArrayList

ArrayList

public ArrayList(Collection<? extends E> collection)
Constructs a new instance of ArrayList containing the elements in the specified collection. The ArrayList will have an initial capacity which is 110% of the size of the collection. The order of the elements in this ArrayList is the order they are returned by the collection iterator.

Parameters:
collection - the collection of elements to add
Method Detail

add

public void add(int location,
                E object)
Inserts the specified object into this ArrayList at the specified location. The object is inserted before any previous element at the specified location. If the location is equal to the size of this ArrayList, the object is added at the end.

Specified by:
add in interface List<E>
Overrides:
add in class AbstractList<E>
Parameters:
location - the index at which to insert
object - the object to add
Throws:
IndexOutOfBoundsException - when location < 0="" ||="">= size()

add

public boolean add(E object)
Adds the specified object at the end of this ArrayList.

Specified by:
add in interface Collection<E>
Specified by:
add in interface List<E>
Overrides:
add in class AbstractList<E>
Parameters:
object - the object to add
Returns:
true

addAll

public boolean addAll(int location,
                      Collection<? extends E> collection)
Inserts the objects in the specified Collection at the specified location in this ArrayList. The objects are added in the order they are returned from the Collection iterator.

Specified by:
addAll in interface List<E>
Overrides:
addAll in class AbstractList<E>
Parameters:
location - the index at which to insert
collection - the Collection of objects
Returns:
true if this ArrayList is modified, false otherwise
Throws:
IndexOutOfBoundsException - when location < 0="" ||=""> size()

addAll

public boolean addAll(Collection<? extends E> collection)
Adds the objects in the specified Collection to this ArrayList.

Specified by:
addAll in interface Collection<E>
Specified by:
addAll in interface List<E>
Overrides:
addAll in class AbstractCollection<E>
Parameters:
collection - the Collection of objects
Returns:
true if this ArrayList is modified, false otherwise

clear

public void clear()
Removes all elements from this ArrayList, leaving it empty.

Specified by:
clear in interface Collection<E>
Specified by:
clear in interface List<E>
Overrides:
clear in class AbstractList<E>
See Also:
isEmpty(), size()

clone

public Object clone()
Returns a new ArrayList with the same elements, size and capacity as this ArrayList.

Overrides:
clone in class Object
Returns:
a shallow copy of this ArrayList
See Also:
Cloneable

contains

public boolean contains(Object object)
Searches this ArrayList for the specified object.

Specified by:
contains in interface Collection<E>
Specified by:
contains in interface List<E>
Overrides:
contains in class AbstractCollection<E>
Parameters:
object - the object to search for
Returns:
true if object is an element of this ArrayList, false otherwise

ensureCapacity

public void ensureCapacity(int minimumCapacity)
Ensures that this ArrayList can hold the specified number of elements without growing.

Parameters:
minimumCapacity - the minimum number of elements that this ArrayList will hold before growing

get

public E get(int location)
Returns the element at the specified location in this ArrayList.

Specified by:
get in interface List<E>
Specified by:
get in class AbstractList<E>
Parameters:
location - the index of the element to return
Returns:
the element at the specified index
Throws:
IndexOutOfBoundsException - when location < 0="" ||="">= size()

indexOf

public int indexOf(Object object)
Searches this ArrayList for the specified object and returns the index of the first occurrence.

Specified by:
indexOf in interface List<E>
Overrides:
indexOf in class AbstractList<E>
Parameters:
object - the object to search for
Returns:
the index of the first occurrence of the object

isEmpty

public boolean isEmpty()
Returns if this ArrayList has no elements, a size of zero.

Specified by:
isEmpty in interface Collection<E>
Specified by:
isEmpty in interface List<E>
Overrides:
isEmpty in class AbstractCollection<E>
Returns:
true if this ArrayList has no elements, false otherwise
See Also:
size()

lastIndexOf

public int lastIndexOf(Object object)
Searches this ArrayList for the specified object and returns the index of the last occurrence.

Specified by:
lastIndexOf in interface List<E>
Overrides:
lastIndexOf in class AbstractList<E>
Parameters:
object - the object to search for
Returns:
the index of the last occurrence of the object

remove

public E remove(int location)
Removes the object at the specified location from this ArrayList.

Specified by:
remove in interface List<E>
Overrides:
remove in class AbstractList<E>
Parameters:
location - the index of the object to remove
Returns:
the removed object
Throws:
IndexOutOfBoundsException - when location < 0="" ||="">= size()

remove

public boolean remove(Object object)
Description copied from class: AbstractCollection
Removes the first occurrence of the specified object from this Collection. This operation traverses over the collection, looking for the specified object. Once the object is found, the object will be removed from the collection using the iterator's remove method. This collection will throw an UnsupportedOperationException if the iterator returned does not implement remove method, and the specified object is in this collection.

Specified by:
remove in interface Collection<E>
Specified by:
remove in interface List<E>
Overrides:
remove in class AbstractCollection<E>
Parameters:
object - the object to remove
Returns:
true if this Collection is modified, false otherwise

removeRange

protected void removeRange(int start,
                           int end)
Removes the objects in the specified range from the start to the end, but not including the end index.

Overrides:
removeRange in class AbstractList<E>
Parameters:
start - the index at which to start removing
end - the index one past the end of the range to remove
Throws:
IndexOutOfBoundsException - when start < 0,="" start=""> end or end > size()

set

public E set(int location,
             E object)
Replaces the element at the specified location in this ArrayList with the specified object.

Specified by:
set in interface List<E>
Overrides:
set in class AbstractList<E>
Parameters:
location - the index at which to put the specified object
object - the object to add
Returns:
the previous element at the index
Throws:
IndexOutOfBoundsException - when location < 0="" ||="">= size()

size

public int size()
Returns the number of elements in this ArrayList.

Specified by:
size in interface Collection<E>
Specified by:
size in interface List<E>
Specified by:
size in class AbstractCollection<E>
Returns:
the number of elements in this ArrayList

toArray

public Object[] toArray()
Returns a new array containing all elements contained in this ArrayList.

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>
Overrides:
toArray in class AbstractCollection<E>
Returns:
an array of the elements from this ArrayList

toArray

public <T> T[] toArray(T[] contents)
Returns an array containing all elements contained in this ArrayList. 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 this ArrayList, the array element following the collection elements is set to null.

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>
Overrides:
toArray in class AbstractCollection<E>
Parameters:
contents - the array
Returns:
an array of the elements from this ArrayList
Throws:
ArrayStoreException - when the type of an element in this ArrayList cannot be stored in the type of the specified array

trimToSize

public void trimToSize()
Sets the capacity of this ArrayList to be the same as the size.

See Also:
size()

Build 1.0_r1(from source)

Please submit a feedback, bug or feature