Package java.util

Class ArrayList<E>

    • Constructor Summary

      Constructors 
      Constructor Description
      ArrayList()
      Constructs an empty list with an initial capacity of ten.
      ArrayList​(int initialCapacity)
      Constructs an empty list with the specified initial capacity.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int index, E element)
      Inserts the specified element at the specified position in this list.
      boolean add​(E e)
      Appends the specified element to the end of this list.
      boolean addAll​(int index, Collection<? extends E> c)
      Inserts all of the elements in the specified collection into this list, starting at the specified position.
      boolean addAll​(Collection<? extends E> 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.
      void clear()
      Removes all of the elements from this list.
      boolean contains​(Object o)
      Returns true if this list contains the specified element.
      boolean containsAll​(Collection<?> arg0)
      Tests whether this List contains all objects contained in the specified collection.
      (package private) E elementData​(int index)  
      E get​(int index)
      Returns the element at the specified position in this list.
      int indexOf​(Object o)
      Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
      (package private) int indexOfRange​(Object o, int start, int end)  
      boolean isEmpty()
      Returns true if this list contains no elements.
      Iterator<E> iterator()
      Returns an iterator over the elements in this list in proper sequence.
      int lastIndexOf​(Object o)
      Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
      (package private) int lastIndexOfRange​(Object o, int start, int end)  
      ListIterator<E> listIterator()
      Returns a list iterator over the elements in this list (in proper sequence).
      ListIterator<E> listIterator​(int index)
      Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
      E remove​(int index)
      Removes the element at the specified position in this list.
      boolean remove​(Object o)
      Removes the first occurrence of the specified element from this list, if it is present.
      boolean removeAll​(Collection<?> c)
      Removes from this list all of its elements that are contained in the specified collection.
      boolean retainAll​(Collection<?> c)
      Retains only the elements in this list that are contained in the specified collection.
      E set​(int index, E element)
      Replaces the element at the specified position in this list with the specified element.
      int size()
      Returns the number of elements in this list.
      List<E> subList​(int arg0, int arg1)
      Returns a List of the specified portion of this List from the given start index to the end index minus one.
      Object[] toArray()
      Returns an array containing all of the elements in this list in proper sequence (from first to last element).
      <T> T[] toArray​(T[] arg0)
      Returns an array containing all elements contained in this List.
    • Constructor Detail

      • ArrayList

        public ArrayList​(int initialCapacity)
        Constructs an empty list with the specified initial capacity.
        Parameters:
        initialCapacity - the initial capacity of the list
        Throws:
        IllegalArgumentException - if the specified initial capacity is negative
      • ArrayList

        public ArrayList()
        Constructs an empty list with an initial capacity of ten.
    • Method Detail

      • size

        public int size()
        Returns the number of elements in this list.
        Specified by:
        size in interface Collection<E>
        Specified by:
        size in interface List<E>
        Returns:
        the number of elements in this list
      • isEmpty

        public boolean isEmpty()
        Returns true if this list contains no elements.
        Specified by:
        isEmpty in interface Collection<E>
        Specified by:
        isEmpty in interface List<E>
        Returns:
        true if this list contains no elements
        See Also:
        List.size()
      • contains

        public boolean contains​(Object o)
        Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that Objects.equals(o, e).
        Specified by:
        contains in interface Collection<E>
        Specified by:
        contains in interface List<E>
        Parameters:
        o - element whose presence in this list is to be tested
        Returns:
        true if this list contains the specified element
      • indexOf

        public int indexOf​(Object o)
        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 i such that Objects.equals(o, get(i)), or -1 if there is no such index.
        Specified by:
        indexOf in interface List<E>
        Parameters:
        o - the object to search for.
        Returns:
        the index of the first occurrence of the object or -1 if the object was not found.
      • indexOfRange

        int indexOfRange​(Object o,
                         int start,
                         int end)
      • lastIndexOf

        public int lastIndexOf​(Object o)
        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 i such that Objects.equals(o, get(i)), or -1 if there is no such index.
        Specified by:
        lastIndexOf in interface List<E>
        Parameters:
        o - the object to search for.
        Returns:
        the index of the last occurrence of the object, or -1 if the object was not found.
      • lastIndexOfRange

        int lastIndexOfRange​(Object o,
                             int start,
                             int end)
      • get

        public E get​(int index)
        Returns the element at the specified position in this list.
        Specified by:
        get in interface List<E>
        Parameters:
        index - index of the element to return
        Returns:
        the element at the specified position in this list
        Throws:
        IndexOutOfBoundsException - if location < 0 || location >= size()
      • set

        public E set​(int index,
                     E element)
        Replaces the element at the specified position in this list with the specified element.
        Specified by:
        set in interface List<E>
        Parameters:
        index - index of the element to replace
        element - element to be stored at the specified position
        Returns:
        the element previously at the specified position
        Throws:
        IndexOutOfBoundsException - if location < 0 || location >= size()
      • add

        public boolean add​(E e)
        Appends the specified element to the end of this list.
        Specified by:
        add in interface Collection<E>
        Specified by:
        add in interface List<E>
        Parameters:
        e - element to be appended to this list
        Returns:
        true (as specified by Collection.add(E))
      • add

        public void add​(int index,
                        E element)
        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:
        add in interface List<E>
        Parameters:
        index - index at which the specified element is to be inserted
        element - element to be inserted
        Throws:
        IndexOutOfBoundsException - if location < 0 || location > size()
      • addAll

        public boolean addAll​(Collection<? extends E> 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:
        addAll in interface Collection<E>
        Specified by:
        addAll in interface List<E>
        Parameters:
        c - collection containing elements to be added to this list
        Returns:
        true if this list changed as a result of the call
        Throws:
        NullPointerException - if the specified collection is null
      • addAll

        public boolean addAll​(int index,
                              Collection<? extends E> 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:
        addAll in interface List<E>
        Parameters:
        index - index at which to insert the first element from the specified collection
        c - collection containing elements to be added to this list
        Returns:
        true if this list changed as a result of the call
        Throws:
        IndexOutOfBoundsException - if location < 0 || location > size()
        NullPointerException - if the specified collection is null
      • containsAll

        public boolean containsAll​(Collection<?> arg0)
        Description copied from interface: List
        Tests whether this List contains all objects contained in the specified collection.
        Specified by:
        containsAll in interface Collection<E>
        Specified by:
        containsAll in interface List<E>
        Parameters:
        arg0 - the collection of objects
        Returns:
        true if all objects in the specified collection are elements of this List, false otherwise.
      • remove

        public E remove​(int index)
        Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).
        Specified by:
        remove in interface List<E>
        Parameters:
        index - the index of the element to be removed
        Returns:
        the element that was removed from the list
        Throws:
        IndexOutOfBoundsException - if location < 0 || location >= size()
      • remove

        public boolean remove​(Object o)
        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 i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).
        Specified by:
        remove in interface Collection<E>
        Specified by:
        remove in interface List<E>
        Parameters:
        o - element to be removed from this list, if present
        Returns:
        true if this list contained the specified element
      • removeAll

        public boolean removeAll​(Collection<?> c)
        Removes from this list all of its elements that are contained in the specified collection.
        Specified by:
        removeAll in interface Collection<E>
        Specified by:
        removeAll in interface List<E>
        Parameters:
        c - collection containing elements to be removed from this list
        Returns:
        true if this list changed as a result of the call
        Throws:
        ClassCastException - if the class of an element of this list is incompatible with the specified collection (optional)
        NullPointerException - 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:
        Collection.contains(Object)
      • retainAll

        public boolean retainAll​(Collection<?> 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:
        retainAll in interface Collection<E>
        Specified by:
        retainAll in interface List<E>
        Parameters:
        c - collection containing elements to be retained in this list
        Returns:
        true if this list changed as a result of the call
        Throws:
        ClassCastException - if the class of an element of this list is incompatible with the specified collection (optional)
        NullPointerException - 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:
        Collection.contains(Object)
      • toArray

        public Object[] toArray()
        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:
        toArray in interface Collection<E>
        Specified by:
        toArray in interface List<E>
        Returns:
        an array containing all of the elements in this list in proper sequence
      • toArray

        public <T> T[] toArray​(T[] arg0)
        Description copied from interface: List
        Returns an array containing all elements contained in this List. 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 List, 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>
        Type Parameters:
        T - the type.
        Parameters:
        arg0 - the array.
        Returns:
        an array of the elements from this List.
      • subList

        public List<E> subList​(int arg0,
                               int arg1)
        Description copied from interface: List
        Returns a List of the specified portion of this List from the given start index to the end index minus one. The returned List is backed by this List so changes to it are reflected by the other.
        Specified by:
        subList in interface List<E>
        Parameters:
        arg0 - the index at which to start the sublist.
        arg1 - the index one past the end of the sublist.
        Returns:
        a list of a portion of this List.
      • iterator

        public Iterator<E> iterator()
        Returns an iterator over the elements in this list in proper sequence.
        Specified by:
        iterator in interface Collection<E>
        Specified by:
        iterator in interface Iterable<E>
        Specified by:
        iterator in interface List<E>
        Returns:
        an iterator over the elements in this list in proper sequence
        See Also:
        Iterator
      • listIterator

        public ListIterator<E> listIterator()
        Returns a list iterator over the elements in this list (in proper sequence).

        The returned list iterator is fail-fast.

        Specified by:
        listIterator in interface List<E>
        Returns:
        a List iterator on the elements of this List
        See Also:
        listIterator(int)
      • listIterator

        public ListIterator<E> listIterator​(int index)
        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 next. An initial call to previous would return the element with the specified index minus one.

        The returned list iterator is fail-fast.

        Specified by:
        listIterator in interface List<E>
        Parameters:
        index - the index at which to start the iteration.
        Returns:
        a list iterator on the elements of this List.
        Throws:
        IndexOutOfBoundsException - if location < 0 || location > size()
        See Also:
        ListIterator
      • elementData

        E elementData​(int index)