FileDocCategorySizeDatePackage
ArrayList.javaAPI DocAndroid 1.5 API23344Wed May 06 22:41:04 BST 2009java.util

ArrayList

public class ArrayList extends AbstractList implements Serializable, RandomAccess, List, Cloneable
ArrayList is an implementation of {@link List}, backed by an array. All optional operations adding, removing, and replacing are supported. The elements can be any objects.
since
Android 1.0

Fields Summary
private static final long
serialVersionUID
private static final Object[]
emptyArray
zero-element array
private transient int
firstIndex
private transient int
lastIndex
private transient E[]
array
private static final ObjectStreamField[]
serialPersistentFields
Constructors Summary
public ArrayList()
Constructs a new instance of {@code ArrayList} with zero capacity.

since
Android 1.0


                       
      
        this(0);
    
public ArrayList(int capacity)
Constructs a new instance of {@code ArrayList} with the specified capacity.

param
capacity the initial capacity of this {@code ArrayList}.
since
Android 1.0

        firstIndex = lastIndex = 0;
        try {
            array = newElementArray(capacity);
        } catch (NegativeArraySizeException e) {
            throw new IllegalArgumentException();
        }
    
public ArrayList(Collection collection)
Constructs a new instance of {@code ArrayList} containing the elements of the specified collection. The initial size of the {@code ArrayList} will be 10% higher than the size of the specified collection.

param
collection the collection of elements to add.
since
Android 1.0

        int size = collection.size();
        firstIndex = lastIndex = 0;
        array = newElementArray(size + (size / 10));
        addAll(collection);
    
Methods Summary
public voidadd(int location, E object)
Inserts the specified object into this {@code 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 {@code ArrayList}, the object is added at the end.

param
location the index at which to insert the object.
param
object the object to add.
throws
IndexOutOfBoundsException when {@code location < 0 || > size()}
since
Android 1.0

        // BEGIN android-changed: slight performance improvement
        int size = lastIndex - firstIndex;
        // END android-changed
        if (0 < location && location < size) {
            if (firstIndex == 0 && lastIndex == array.length) {
                growForInsert(location, 1);
            } else if ((location < size / 2 && firstIndex > 0)
                    || lastIndex == array.length) {
                System.arraycopy(array, firstIndex, array, --firstIndex,
                        location);
            } else {
                int index = location + firstIndex;
                System.arraycopy(array, index, array, index + 1, size
                        - location);
                lastIndex++;
            }
            array[location + firstIndex] = object;
        } else if (location == 0) {
            if (firstIndex == 0) {
                growAtFront(1);
            }
            array[--firstIndex] = object;
        } else if (location == size) {
            if (lastIndex == array.length) {
                growAtEnd(1);
            }
            array[lastIndex++] = object;
        } else {
            throw new IndexOutOfBoundsException();
        }

        modCount++;
    
public booleanadd(E object)
Adds the specified object at the end of this {@code ArrayList}.

param
object the object to add.
return
always true
since
Android 1.0

        if (lastIndex == array.length) {
            growAtEnd(1);
        }
        array[lastIndex++] = object;
        modCount++;
        return true;
    
public booleanaddAll(int location, java.util.Collection collection)
Inserts the objects in the specified collection at the specified location in this List. The objects are added in the order they are returned from the collection's iterator.

param
location the index at which to insert.
param
collection the collection of objects.
return
{@code true} if this {@code ArrayList} is modified, {@code false} otherwise.
throws
IndexOutOfBoundsException when {@code location < 0 || > size()}
since
Android 1.0

        int size = size();
        if (location < 0 || location > size) {
            throw new IndexOutOfBoundsException();
        }
        int growSize = collection.size();
        if (0 < location && location < size) {
            if (array.length - size < growSize) {
                growForInsert(location, growSize);
            } else if ((location < size / 2 && firstIndex > 0)
                    || lastIndex > array.length - growSize) {
                int newFirst = firstIndex - growSize;
                if (newFirst < 0) {
                    int index = location + firstIndex;
                    System.arraycopy(array, index, array, index - newFirst,
                            size - location);
                    lastIndex -= newFirst;
                    newFirst = 0;
                }
                System.arraycopy(array, firstIndex, array, newFirst, location);
                firstIndex = newFirst;
            } else {
                int index = location + firstIndex;
                System.arraycopy(array, index, array, index + growSize, size
                        - location);
                lastIndex += growSize;
            }
        } else if (location == 0) {
            growAtFront(growSize);
            firstIndex -= growSize;
        } else if (location == size) {
            if (lastIndex > array.length - growSize) {
                growAtEnd(growSize);
            }
            lastIndex += growSize;
        }

        if (growSize > 0) {
            Iterator<? extends E> it = collection.iterator();
            int index = location + firstIndex;
            int end = index + growSize;
            while (index < end) {
                array[index++] = it.next();
            }
            modCount++;
            return true;
        }
        return false;
    
public booleanaddAll(java.util.Collection collection)
Adds the objects in the specified collection to this {@code ArrayList}.

param
collection the collection of objects.
return
{@code true} if this {@code ArrayList} is modified, {@code false} otherwise.
since
Android 1.0

        int growSize = collection.size();
        if (growSize > 0) {
            if (lastIndex > array.length - growSize) {
                growAtEnd(growSize);
            }
            Iterator<? extends E> it = collection.iterator();
            int end = lastIndex + growSize;
            while (lastIndex < end) {
                array[lastIndex++] = it.next();
            }
            modCount++;
            return true;
        }
        return false;
    
public voidclear()
Removes all elements from this {@code ArrayList}, leaving it empty.

see
#isEmpty
see
#size
since
Android 1.0

        if (firstIndex != lastIndex) {
            Arrays.fill(array, firstIndex, lastIndex, null);
            firstIndex = lastIndex = 0;
            modCount++;
        }
    
public java.lang.Objectclone()
Returns a new {@code ArrayList} with the same elements, the same size and the same capacity as this {@code ArrayList}.

return
a shallow copy of this {@code ArrayList}
see
java.lang.Cloneable
since
Android 1.0

        try {
            ArrayList<E> newList = (ArrayList<E>) super.clone();
            newList.array = array.clone();
            return newList;
        } catch (CloneNotSupportedException e) {
            return null;
        }
    
public booleancontains(java.lang.Object object)
Searches this {@code ArrayList} for the specified object.

param
object the object to search for.
return
{@code true} if {@code object} is an element of this {@code ArrayList}, {@code false} otherwise
since
Android 1.0

        if (object != null) {
            for (int i = firstIndex; i < lastIndex; i++) {
                if (object.equals(array[i])) {
                    return true;
                }
            }
        } else {
            for (int i = firstIndex; i < lastIndex; i++) {
                if (array[i] == null) {
                    return true;
                }
            }
        }
        return false;
    
public voidensureCapacity(int minimumCapacity)
Ensures that after this operation the {@code ArrayList} can hold the specified number of elements without further growing.

param
minimumCapacity the minimum capacity asked for.
since
Android 1.0

        if (array.length < minimumCapacity) {
            if (firstIndex > 0) {
                growAtFront(minimumCapacity - array.length);
            } else {
                growAtEnd(minimumCapacity - array.length);
            }
        }
    
public Eget(int location)

        // BEGIN android-changed: slight performance improvement
        int _firstIndex = firstIndex;
        if (0 <= location && location < lastIndex - _firstIndex) {
            return array[_firstIndex + location];
        }
        throw new IndexOutOfBoundsException("Invalid location " + location
                + ", size is " + (lastIndex - _firstIndex));
        // END android-changed
    
private voidgrowAtEnd(int required)

        int size = size();
        if (firstIndex >= required - (array.length - lastIndex)) {
            int newLast = lastIndex - firstIndex;
            if (size > 0) {
                System.arraycopy(array, firstIndex, array, 0, size);
                int start = newLast < firstIndex ? firstIndex : newLast;
                Arrays.fill(array, start, array.length, null);
            }
            firstIndex = 0;
            lastIndex = newLast;
        } else {
            int increment = size / 2;
            if (required > increment) {
                increment = required;
            }
            if (increment < 12) {
                increment = 12;
            }
            E[] newArray = newElementArray(size + increment);
            if (size > 0) {
                System.arraycopy(array, firstIndex, newArray, 0, size);
                firstIndex = 0;
                lastIndex = size;
            }
            array = newArray;
        }
    
private voidgrowAtFront(int required)

        int size = size();
        if (array.length - lastIndex >= required) {
            int newFirst = array.length - size;
            if (size > 0) {
                System.arraycopy(array, firstIndex, array, newFirst, size);
                int length = firstIndex + size > newFirst ? newFirst
                        : firstIndex + size;
                Arrays.fill(array, firstIndex, length, null);
            }
            firstIndex = newFirst;
            lastIndex = array.length;
        } else {
            int increment = size / 2;
            if (required > increment) {
                increment = required;
            }
            if (increment < 12) {
                increment = 12;
            }
            E[] newArray = newElementArray(size + increment);
            if (size > 0) {
                System.arraycopy(array, firstIndex, newArray, newArray.length
                        - size, size);
            }
            firstIndex = newArray.length - size;
            lastIndex = newArray.length;
            array = newArray;
        }
    
private voidgrowForInsert(int location, int required)

        int size = size(), increment = size / 2;
        if (required > increment) {
            increment = required;
        }
        if (increment < 12) {
            increment = 12;
        }
        E[] newArray = newElementArray(size + increment);
        if (location < size / 2) {
            int newFirst = newArray.length - (size + required);
            System.arraycopy(array, location, newArray, location + increment,
                    size - location);
            System.arraycopy(array, firstIndex, newArray, newFirst, location);
            firstIndex = newFirst;
            lastIndex = newArray.length;
        } else {
            System.arraycopy(array, firstIndex, newArray, 0, location);
            System.arraycopy(array, location, newArray, location + required,
                    size - location);
            firstIndex = 0;
            lastIndex += required;
        }
        array = newArray;
    
public intindexOf(java.lang.Object object)

        if (object != null) {
            for (int i = firstIndex; i < lastIndex; i++) {
                if (object.equals(array[i])) {
                    return i - firstIndex;
                }
            }
        } else {
            for (int i = firstIndex; i < lastIndex; i++) {
                if (array[i] == null) {
                    return i - firstIndex;
                }
            }
        }
        return -1;
    
public booleanisEmpty()

        return lastIndex == firstIndex;
    
public intlastIndexOf(java.lang.Object object)

        if (object != null) {
            for (int i = lastIndex - 1; i >= firstIndex; i--) {
                if (object.equals(array[i])) {
                    return i - firstIndex;
                }
            }
        } else {
            for (int i = lastIndex - 1; i >= firstIndex; i--) {
                if (array[i] == null) {
                    return i - firstIndex;
                }
            }
        }
        return -1;
    
private E[]newElementArray(int size)

        // BEGIN android-added
        if (size == 0) {
            return (E[])emptyArray;
        }
        // END android-added

        return (E[])new Object[size];
    
private voidreadObject(java.io.ObjectInputStream stream)

        ObjectInputStream.GetField fields = stream.readFields();
        lastIndex = fields.get("size", 0); //$NON-NLS-1$
        array = newElementArray(stream.readInt());
        for (int i = 0; i < lastIndex; i++) {
            array[i] = (E)stream.readObject();
        }
    
public Eremove(int location)
Removes the object at the specified location from this list.

param
location the index of the object to remove.
return
the removed object.
throws
IndexOutOfBoundsException when {@code location < 0 || >= size()}
since
Android 1.0

        E result;
        // BEGIN android-changed: slight performance improvement
        int size = lastIndex - firstIndex;
        // END android-changed
        if (0 <= location && location < size) {
            if (location == size - 1) {
                result = array[--lastIndex];
                array[lastIndex] = null;
            } else if (location == 0) {
                result = array[firstIndex];
                array[firstIndex++] = null;
            } else {
                int elementIndex = firstIndex + location;
                result = array[elementIndex];
                if (location < size / 2) {
                    System.arraycopy(array, firstIndex, array, firstIndex + 1,
                            location);
                    array[firstIndex++] = null;
                } else {
                    System.arraycopy(array, elementIndex + 1, array,
                            elementIndex, size - location - 1);
                    array[--lastIndex] = null;
                }
            }
        } else {
            throw new IndexOutOfBoundsException();
        }

        modCount++;
        return result;
    
public booleanremove(java.lang.Object object)

        int index = indexOf(object);
        if (index >= 0) {
            remove(index);
            return true;
        }
        return false;
    
protected voidremoveRange(int start, int end)
Removes the objects in the specified range from the start to the end, but not including the end index.

param
start the index at which to start removing.
param
end the index one after the end of the range to remove.
throws
IndexOutOfBoundsException when {@code start < 0, start > end} or {@code end > size()}
since
Android 1.0

        if (start >= 0 && start <= end && end <= size()) {
            if (start == end) {
                return;
            }
            int size = size();
            if (end == size) {
                Arrays.fill(array, firstIndex + start, lastIndex, null);
                lastIndex = firstIndex + start;
            } else if (start == 0) {
                Arrays.fill(array, firstIndex, firstIndex + end, null);
                firstIndex += end;
            } else {
                System.arraycopy(array, firstIndex + end, array, firstIndex
                        + start, size - end);
                int newLast = lastIndex + start - end;
                Arrays.fill(array, newLast, lastIndex, null);
                lastIndex = newLast;
            }
            modCount++;
        } else {
            throw new IndexOutOfBoundsException();
        }
    
public Eset(int location, E object)
Replaces the element at the specified location in this {@code ArrayList} with the specified object.

param
location the index at which to put the specified object.
param
object the object to add.
return
the previous element at the index.
throws
IndexOutOfBoundsException when {@code location < 0 || >= size()}
since
Android 1.0

        // BEGIN android-changed: slight performance improvement
        if (0 <= location && location < (lastIndex - firstIndex)) {
        // END android-changed
            E result = array[firstIndex + location];
            array[firstIndex + location] = object;
            return result;
        }
        throw new IndexOutOfBoundsException();
    
public intsize()
Returns the number of elements in this {@code ArrayList}.

return
the number of elements in this {@code ArrayList}.
since
Android 1.0

        return lastIndex - firstIndex;
    
public java.lang.Object[]toArray()
Returns a new array containing all elements contained in this {@code ArrayList}.

return
an array of the elements from this {@code ArrayList}
since
Android 1.0

        int size = size();
        Object[] result = new Object[size];
        System.arraycopy(array, firstIndex, result, 0, size);
        return result;
    
public T[]toArray(T[] contents)
Returns an array containing all elements contained in this {@code 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 {@code ArrayList}, the array element following the collection elements is set to null.

param
contents the array.
return
an array of the elements from this {@code ArrayList}.
throws
ArrayStoreException when the type of an element in this {@code ArrayList} cannot be stored in the type of the specified array.
since
Android 1.0

        int size = size();
        if (size > contents.length) {
            Class<?> ct = contents.getClass().getComponentType();
            contents = (T[]) Array.newInstance(ct, size);
        }
        System.arraycopy(array, firstIndex, contents, 0, size);
        if (size < contents.length) {
            contents[size] = null;
        }
        return contents;
    
public voidtrimToSize()
Sets the capacity of this {@code ArrayList} to be the same as the current size.

see
#size
since
Android 1.0

        int size = size();
        E[] newArray = newElementArray(size);
        System.arraycopy(array, firstIndex, newArray, 0, size);
        array = newArray;
        firstIndex = 0;
        lastIndex = array.length;
    
private voidwriteObject(java.io.ObjectOutputStream stream)

 //$NON-NLS-1$

          
        ObjectOutputStream.PutField fields = stream.putFields();
        fields.put("size", size()); //$NON-NLS-1$
        stream.writeFields();
        stream.writeInt(array.length);
        Iterator<?> it = iterator();
        while (it.hasNext()) {
            stream.writeObject(it.next());
        }