FileDocCategorySizeDatePackage
LinkedList.javaAPI DocAndroid 1.5 API24960Wed May 06 22:41:04 BST 2009java.util

LinkedList

public class LinkedList extends AbstractSequentialList implements Serializable, List, Cloneable, Queue
LinkedList is an implementation of List, backed by a linked list. 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
transient int
size
transient Link
voidLink
Constructors Summary
public LinkedList()
Constructs a new empty instance of {@code LinkedList}.

since
Android 1.0

        voidLink = new Link<E>(null, null, null);
        voidLink.previous = voidLink;
        voidLink.next = voidLink;
    
public LinkedList(Collection collection)
Constructs a new instance of {@code LinkedList} that holds all of the elements contained in the specified {@code collection}. The order of the elements in this new {@code LinkedList} will be determined by the iteration order of {@code collection}.

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

        this();
        addAll(collection);
    
Methods Summary
public voidadd(int location, E object)
Inserts the specified object into this {@code LinkedList} 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 LinkedList}, the object is added at the end.

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

        if (0 <= location && location <= size) {
            Link<E> link = voidLink;
            if (location < (size / 2)) {
                for (int i = 0; i <= location; i++) {
                    link = link.next;
                }
            } else {
                for (int i = size; i > location; i--) {
                    link = link.previous;
                }
            }
            Link<E> previous = link.previous;
            Link<E> newLink = new Link<E>(object, previous, link);
            previous.next = newLink;
            link.previous = newLink;
            size++;
            modCount++;
        } else {
            throw new IndexOutOfBoundsException();
        }
    
public booleanadd(E object)
Adds the specified object at the end of this {@code LinkedList}.

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

        // Cannot call addLast() as subclasses can override
        Link<E> oldLast = voidLink.previous;
        Link<E> newLink = new Link<E>(object, oldLast, voidLink);
        voidLink.previous = newLink;
        oldLast.next = newLink;
        size++;
        modCount++;
        return true;
    
public booleanaddAll(int location, java.util.Collection collection)
Inserts the objects in the specified collection at the specified location in this {@code LinkedList}. 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 LinkedList} is modified, {@code false} otherwise.
throws
ClassCastException if the class of an object is inappropriate for this list.
throws
IllegalArgumentException if an object cannot be added to this list.
throws
IndexOutOfBoundsException if {@code location < 0 || > size()}
since
Android 1.0

        if (location < 0 || location > size) {
            throw new IndexOutOfBoundsException();
        }
        int adding = collection.size();
        if (adding == 0) {
            return false;
        }
        Link<E> previous = voidLink;
        if (location < (size / 2)) {
            for (int i = 0; i < location; i++) {
                previous = previous.next;
            }
        } else {
            for (int i = size; i >= location; i--) {
                previous = previous.previous;
            }
        }
        Link<E> next = previous.next;
        for (E e : collection) {
            Link<E> newLink = new Link<E>(e, previous, null);
            previous.next = newLink;
            previous = newLink;
        }
        previous.next = next;
        next.previous = previous;
        size += adding;
        modCount++;
        return true;
    
public booleanaddAll(java.util.Collection collection)
Adds the objects in the specified Collection to this {@code LinkedList}.

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

        int adding = collection.size();
        if (adding == 0) {
            return false;
        }
        Link<E> previous = voidLink.previous;
        for (E e : collection) {
            Link<E> newLink = new Link<E>(e, previous, null);
            previous.next = newLink;
            previous = newLink;
        }
        previous.next = voidLink;
        voidLink.previous = previous;
        size += adding;
        modCount++;
        return true;
    
public voidaddFirst(E object)
Adds the specified object at the beginning of this {@code LinkedList}.

param
object the object to add.
since
Android 1.0

        Link<E> oldFirst = voidLink.next;
        Link<E> newLink = new Link<E>(object, voidLink, oldFirst);
        voidLink.next = newLink;
        oldFirst.previous = newLink;
        size++;
        modCount++;
    
public voidaddLast(E object)
Adds the specified object at the end of this {@code LinkedList}.

param
object the object to add.
since
Android 1.0

        Link<E> oldLast = voidLink.previous;
        Link<E> newLink = new Link<E>(object, oldLast, voidLink);
        voidLink.previous = newLink;
        oldLast.next = newLink;
        size++;
        modCount++;
    
public voidclear()
Removes all elements from this {@code LinkedList}, leaving it empty.

see
List#isEmpty
see
#size
since
Android 1.0

        if (size > 0) {
            size = 0;
            voidLink.next = voidLink;
            voidLink.previous = voidLink;
            modCount++;
        }
    
public java.lang.Objectclone()
Returns a new {@code LinkedList} with the same elements and size as this {@code LinkedList}.

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

        try {
            LinkedList<E> l = (LinkedList<E>) super.clone();
            l.size = 0;
            l.voidLink = new Link<E>(null, null, null);
            l.voidLink.previous = l.voidLink;
            l.voidLink.next = l.voidLink;
            l.addAll(this);
            return l;
        } catch (CloneNotSupportedException e) {
            return null;
        }
    
public booleancontains(java.lang.Object object)
Searches this {@code LinkedList} for the specified object.

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

        Link<E> link = voidLink.next;
        if (object != null) {
            while (link != voidLink) {
                if (object.equals(link.data)) {
                    return true;
                }
                link = link.next;
            }
        } else {
            while (link != voidLink) {
                if (link.data == null) {
                    return true;
                }
                link = link.next;
            }
        }
        return false;
    
public Eelement()

        return getFirst();
    
public Eget(int location)

        if (0 <= location && location < size) {
            Link<E> link = voidLink;
            if (location < (size / 2)) {
                for (int i = 0; i <= location; i++) {
                    link = link.next;
                }
            } else {
                for (int i = size; i > location; i--) {
                    link = link.previous;
                }
            }
            return link.data;
        }
        throw new IndexOutOfBoundsException();
    
public EgetFirst()
Returns the first element in this {@code LinkedList}.

return
the first element.
throws
NoSuchElementException if this {@code LinkedList} is empty.
since
Android 1.0

        Link<E> first = voidLink.next;
        if (first != voidLink) {
            return first.data;
        }
        throw new NoSuchElementException();
    
public EgetLast()
Returns the last element in this {@code LinkedList}.

return
the last element
throws
NoSuchElementException if this {@code LinkedList} is empty
since
Android 1.0

        Link<E> last = voidLink.previous;
        if (last != voidLink) {
            return last.data;
        }
        throw new NoSuchElementException();
    
public intindexOf(java.lang.Object object)

        int pos = 0;
        Link<E> link = voidLink.next;
        if (object != null) {
            while (link != voidLink) {
                if (object.equals(link.data)) {
                    return pos;
                }
                link = link.next;
                pos++;
            }
        } else {
            while (link != voidLink) {
                if (link.data == null) {
                    return pos;
                }
                link = link.next;
                pos++;
            }
        }
        return -1;
    
public intlastIndexOf(java.lang.Object object)
Searches this {@code LinkedList} for the specified object and returns the index of the last occurrence.

param
object the object to search for
return
the index of the last occurrence of the object, or -1 if it was not found.
since
Android 1.0

        int pos = size;
        Link<E> link = voidLink.previous;
        if (object != null) {
            while (link != voidLink) {
                pos--;
                if (object.equals(link.data)) {
                    return pos;
                }
                link = link.previous;
            }
        } else {
            while (link != voidLink) {
                pos--;
                if (link.data == null) {
                    return pos;
                }
                link = link.previous;
            }
        }
        return -1;
    
public java.util.ListIteratorlistIterator(int location)
Returns a ListIterator on the elements of this {@code LinkedList}. The elements are iterated in the same order that they occur in the {@code LinkedList}. The iteration starts at the specified location.

param
location the index at which to start the iteration
return
a ListIterator on the elements of this {@code LinkedList}
throws
IndexOutOfBoundsException if {@code location < 0 || >= size()}
see
ListIterator
since
Android 1.0

        return new LinkIterator<E>(this, location);
    
public booleanoffer(E o)

        add(o);
        return true;
    
public Epeek()

        Link<E> first = voidLink.next;
        return first == voidLink ? null : first.data;
    
public Epoll()

        return size == 0 ? null : removeFirst();
    
private voidreadObject(java.io.ObjectInputStream stream)

        stream.defaultReadObject();
        size = stream.readInt();
        voidLink = new Link<E>(null, null, null);
        Link<E> link = voidLink;
        for (int i = size; --i >= 0;) {
            Link<E> nextLink = new Link<E>((E)stream.readObject(), link, null);
            link.next = nextLink;
            link = nextLink;
        }
        link.next = voidLink;
        voidLink.previous = link;
    
public Eremove(int location)
Removes the object at the specified location from this {@code LinkedList}.

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

        if (0 <= location && location < size) {
            Link<E> link = voidLink;
            if (location < (size / 2)) {
                for (int i = 0; i <= location; i++) {
                    link = link.next;
                }
            } else {
                for (int i = size; i > location; i--) {
                    link = link.previous;
                }
            }
            Link<E> previous = link.previous;
            Link<E> next = link.next;
            previous.next = next;
            next.previous = previous;
            size--;
            modCount++;
            return link.data;
        }
        throw new IndexOutOfBoundsException();
    
public booleanremove(java.lang.Object object)

        Link<E> link = voidLink.next;
        if (object != null) {
            while (link != voidLink && !object.equals(link.data)) {
                link = link.next;
            }
        } else {
            while (link != voidLink && link.data != null) {
                link = link.next;
            }
        }
        if (link == voidLink) {
            return false;
        }
        Link<E> next = link.next;
        Link<E> previous = link.previous;
        previous.next = next;
        next.previous = previous;
        size--;
        modCount++;
        return true;
    
public Eremove()

        return removeFirst();
    
public EremoveFirst()
Removes the first object from this {@code LinkedList}.

return
the removed object.
throws
NoSuchElementException if this {@code LinkedList} is empty.
since
Android 1.0

        Link<E> first = voidLink.next;
        if (first != voidLink) {
            Link<E> next = first.next;
            voidLink.next = next;
            next.previous = voidLink;
            size--;
            modCount++;
            return first.data;
        }
        throw new NoSuchElementException();
    
public EremoveLast()
Removes the last object from this {@code LinkedList}.

return
the removed object.
throws
NoSuchElementException if this {@code LinkedList} is empty.
since
Android 1.0

        Link<E> last = voidLink.previous;
        if (last != voidLink) {
            Link<E> previous = last.previous;
            voidLink.previous = previous;
            previous.next = voidLink;
            size--;
            modCount++;
            return last.data;
        }
        throw new NoSuchElementException();
    
public Eset(int location, E object)
Replaces the element at the specified location in this {@code LinkedList} 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
ClassCastException if the class of an object is inappropriate for this list.
throws
IllegalArgumentException if an object cannot be added to this list.
throws
IndexOutOfBoundsException if {@code location < 0 || >= size()}
since
Android 1.0

        if (0 <= location && location < size) {
            Link<E> link = voidLink;
            if (location < (size / 2)) {
                for (int i = 0; i <= location; i++) {
                    link = link.next;
                }
            } else {
                for (int i = size; i > location; i--) {
                    link = link.previous;
                }
            }
            E result = link.data;
            link.data = object;
            return result;
        }
        throw new IndexOutOfBoundsException();
    
public intsize()
Returns the number of elements in this {@code LinkedList}.

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

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

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

        int index = 0;
        Object[] contents = new Object[size];
        Link<E> link = voidLink.next;
        while (link != voidLink) {
            contents[index++] = link.data;
            link = link.next;
        }
        return contents;
    
public T[]toArray(T[] contents)
Returns an array containing all elements contained in this {@code LinkedList}. 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 LinkedList}, the array element following the collection elements is set to null.

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

        int index = 0;
        if (size > contents.length) {
            Class<?> ct = contents.getClass().getComponentType();
            contents = (T[]) Array.newInstance(ct, size);
        }
        Link<E> link = voidLink.next;
        while (link != voidLink) {
            contents[index++] = (T)link.data;
            link = link.next;
        }
        if (index < contents.length) {
            contents[index] = null;
        }
        return contents;
    
private voidwriteObject(java.io.ObjectOutputStream stream)

        stream.defaultWriteObject();
        stream.writeInt(size);
        Iterator<E> it = iterator();
        while (it.hasNext()) {
            stream.writeObject(it.next());
        }