# Class AbstractList<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>

All Implemented Interfaces:
  `Iterable<E>, Collection<E>, List<E>`

Direct Known Subclasses:
  `AbstractSequentialList, ArrayList, Vector`

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>

`AbstractList` is an abstract implementation of the `List` interface, optimized for a backing store which supports random access. This implementation does not support adding or replacing. A subclass must implement the abstract methods `get()` and `size()`, and to create a modifiable `List` it's necessary to override the `add()` method that currently throws an `UnsupportedOperationException`.

##### Since

1.2

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected int`
  `modCount`
  A counter for changes to the list.

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `AbstractList()`
  Constructs a new instance of this AbstractList.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `add(int location, E object)`
  Inserts the specified object into this List at the specified location.
  `boolean`
  `add(E object)`
  Adds the specified object at the end of this List.
  `boolean`
  `addAll(int location, Collection<? extends E> collection)`
  Inserts the objects in the specified Collection at the specified location in this List.
  `void`
  `clear()`
  Removes all elements from this list, leaving it empty.
  `boolean`
  `equals(java.lang.Object object)`
  Compares the specified object to this list and return true if they are equal.
  `abstract E`
  `get(int location)`
  Returns the element at the specified location in this list.
  `int`
  `hashCode()`
  Returns the hash code of this list.
  `int`
  `indexOf(java.lang.Object object)`
  Searches this list for the specified object and returns the index of the first occurrence.
  `Iterator<E>`
  `iterator()`
  Returns an iterator on the elements of this list.
  `int`
  `lastIndexOf(java.lang.Object object)`
  Searches this list for the specified object and returns the index of the last occurrence.
  `ListIterator<E>`
  `listIterator()`
  Returns a ListIterator on the elements of this list.
  `ListIterator<E>`
  `listIterator(int location)`
  Returns a list iterator on the elements of this list.
  `E`
  `remove(int location)`
  Removes the object at the specified location from this list.
  `protected void`
  `removeRange(int start, int end)`
  Removes the objects in the specified range from the start to the end index minus one.
  `E`
  `set(int location, E object)`
  Replaces the element at the specified location in this list with the specified object.
  `List<E>`
  `subList(int start, int end)`
  Returns a part of consecutive elements of this list as a view.
  `java.lang.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`.

  ### Methods inherited from class AbstractCollection

  `addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, size, toString`

  ### Methods inherited from class java.lang.Object

  `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

  ### Methods inherited from interface Collection

  `removeIf`

  ### Methods inherited from interface Iterable

  `forEach`

  ### Methods inherited from interface List

  `addAll, contains, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, size, sort`

- ## Field Details

  - ### modCount

    protected transient int modCount
    A counter for changes to the list.

- ## Constructor Details

  - ### AbstractList

    protected AbstractList()
    Constructs a new instance of this AbstractList.

- ## Method Details

  - ### add

    public void add(int location, E object)

    Inserts the specified object into this List 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 List, the object is added at the end.

    Concrete implementations that would like to support the add functionality must override this method.

    ###### Parameters

    - `location`: the index at which to insert.

    - `object`: the object to add.

    ###### Throws

    - `UnsupportedOperationException`: if adding to this List is not supported.

    - `ClassCastException`: @throws ClassCastException if the class of the object is inappropriate for this List

    - `IllegalArgumentException`: if the object cannot be added to this List

    - `IndexOutOfBoundsException`: if `location = size()`

    Specified by:
      `add` in interface `List<E>`

  - ### add

    public boolean add(E object)

    Adds the specified object at the end of this List.

    ###### Parameters

    - `object`: the object to add

    ###### Returns

    true

    ###### Throws

    - `UnsupportedOperationException`: if adding to this List is not supported

    - `ClassCastException`: @throws ClassCastException if the class of the object is inappropriate for this List

    - `IllegalArgumentException`: if the object cannot be added to this List

    Specified by:
      `add` in interface `Collection<E>`
    Specified by:
      `add` in interface `List<E>`
    Overrides:
      `add` in class `AbstractCollection<E>`
    Returns:

      `true` if this `Collection` is modified, `false` otherwise.

      ###### Throws

      - `UnsupportedOperationException`: if adding to this `Collection` is not supported.

      - `ClassCastException`: @throws ClassCastException if the class of the object is inappropriate for this collection.

      - `IllegalArgumentException`: if the object cannot be added to this `Collection`.

      - `NullPointerException`: if null elements cannot be added to the `Collection`.

  - ### addAll

    public boolean addAll(int location, Collection<? extends E> 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.

    ###### Parameters

    - `location`: the index at which to insert.

    - `collection`: the Collection of objects

    ###### Returns

    `true` if this List is modified, `false` otherwise.

    ###### Throws

    - `UnsupportedOperationException`: if adding to this list is not supported.

    - `ClassCastException`: if the class of an object is inappropriate for this list.

    - `IllegalArgumentException`: if an object cannot be added to this list.

    - `IndexOutOfBoundsException`: if `location size()`

    Specified by:
      `addAll` in interface `List<E>`
    Returns:

      true if this `List` has been modified through the insertion, false otherwise (i.e. if the passed collection was empty).

      ###### Throws

      - `UnsupportedOperationException`: if adding to this `List` is not supported.

      - `ClassCastException`: @throws ClassCastException if the class of an object is inappropriate for this `List`.

      - `IllegalArgumentException`: if an object cannot be added to this `List`.

      - `IndexOutOfBoundsException`: if `location size()`

  - ### clear

    public void clear()

    Removes all elements from this list, leaving it empty.

    ###### Throws

    - `UnsupportedOperationException`: if removing from this list is not supported.

    ###### See also

    - List#isEmpty

    - List#size

    Specified by:
      `clear` in interface `Collection<E>`
    Specified by:
      `clear` in interface `List<E>`
    Overrides:
      `clear` in class `AbstractCollection<E>`

  - ### equals

    public boolean equals(java.lang.Object object)

    Compares the specified object to this list and return true if they are equal. Two lists are equal when they both contain the same objects in the same order.

    ###### Parameters

    - `object`: the object to compare to this object.

    ###### Returns

    Specified by:
      `equals` in interface `Collection<E>`
    Specified by:
      `equals` in interface `List<E>`
    Overrides:
      `equals` in class `java.lang.Object`
    Returns:

      `true` if the specified object is equal to this list, `false` otherwise.

      ###### See also

      - #hashCode

  - ### get

    public abstract E get(int location)

    Returns the element at the specified location in this list.

    ###### Parameters

    - `location`: the index of the element to return.

    ###### Returns

    the element at the specified index.

    ###### Throws

    - `IndexOutOfBoundsException`: if `location = size()`
    Specified by:
      `get` in interface `List<E>`

  - ### hashCode

    public int hashCode()

    Returns the hash code of this list. The hash code is calculated by taking each element's hashcode into account.

    ###### Returns

    the hash code.

    ###### See also

    - #equals

    - List#hashCode()

    Specified by:
      `hashCode` in interface `Collection<E>`
    Specified by:
      `hashCode` in interface `List<E>`
    Overrides:
      `hashCode` in class `java.lang.Object`

  - ### indexOf

    public int indexOf(java.lang.Object object)

    Searches this list for the specified object and returns the index of the first occurrence.

    ###### Parameters

    - `object`: the object to search for.

    ###### Returns

    Specified by:
      `indexOf` in interface `List<E>`
    Returns:
      the index of the first occurrence of the object, or -1 if it was not found.

  - ### iterator

    public Iterator<E> iterator()

    Returns an iterator on the elements of this list. The elements are iterated in the same order as they occur in the list.

    ###### Returns

    an iterator on the elements of this list.

    ###### See also

    - Iterator
    Specified by:
      `iterator` in interface `Collection<E>`
    Specified by:
      `iterator` in interface `Iterable<E>`
    Specified by:
      `iterator` in interface `List<E>`
    Specified by:
      `iterator` in class `AbstractCollection<E>`

  - ### lastIndexOf

    public int lastIndexOf(java.lang.Object object)

    Searches this list for the specified object and returns the index of the last occurrence.

    ###### Parameters

    - `object`: the object to search for.

    ###### Returns

    Specified by:
      `lastIndexOf` in interface `List<E>`
    Returns:
      the index of the last occurrence of the object, or -1 if the object was not found.

  - ### listIterator

    public ListIterator<E> listIterator()

    Returns a ListIterator on the elements of this list. The elements are iterated in the same order that they occur in the list.

    ###### Returns

    a ListIterator on the elements of this list

    ###### See also

    - ListIterator
    Specified by:
      `listIterator` in interface `List<E>`

  - ### listIterator

    public ListIterator<E> listIterator(int location)

    Returns a list iterator on the elements of this list. The elements are iterated in the same order as they occur in the list. The iteration starts at the specified location.

    ###### Parameters

    - `location`: the index at which to start the iteration.

    ###### Returns

    a ListIterator on the elements of this list.

    ###### Throws

    - `IndexOutOfBoundsException`: if `location size()`

    ###### See also

    - ListIterator
    Specified by:
      `listIterator` in interface `List<E>`

  - ### remove

    public E remove(int location)

    Removes the object at the specified location from this list.

    ###### Parameters

    - `location`: the index of the object to remove.

    ###### Returns

    the removed object.

    ###### Throws

    - `UnsupportedOperationException`: if removing from this list is not supported.

    - `IndexOutOfBoundsException`: if `location = size()`

    Specified by:
      `remove` in interface `List<E>`

  - ### removeRange

    protected void removeRange(int start, int end)

    Removes the objects in the specified range from the start to the end index minus one.

    ###### Parameters

    - `start`: the index at which to start removing.

    - `end`: the index after the last element to remove.

    ###### Throws

    - `UnsupportedOperationException`: if removing from this list is not supported.

    - `IndexOutOfBoundsException`: if `start = size()`.

  - ### set

    public E set(int location, E object)

    Replaces the element at the specified location in this list with the specified object.

    ###### Parameters

    - `location`: the index at which to put the specified object.

    - `object`: the object to add.

    ###### Returns

    the previous element at the index.

    ###### Throws

    - `UnsupportedOperationException`: if replacing elements in this list is not supported.

    - `ClassCastException`: if the class of an object is inappropriate for this list.

    - `IllegalArgumentException`: if an object cannot be added to this list.

    - `IndexOutOfBoundsException`: if `location = size()`

    Specified by:
      `set` in interface `List<E>`

  - ### subList

    public List<E> subList(int start, int end)

    Returns a part of consecutive elements of this list as a view. The returned view will be of zero length if start equals end. Any change that occurs in the returned subList will be reflected to the original list, and vice-versa. All the supported optional operations by the original list will also be supported by this subList.

    This method can be used as a handy method to do some operations on a sub range of the original list, for example `list.subList(from, to).clear();`

    If the original list is modified in other ways than through the returned subList, the behavior of the returned subList becomes undefined.

    The returned subList is a subclass of AbstractList. The subclass stores offset, size of itself, and modCount of the original list. If the original list implements RandomAccess interface, the returned subList also implements RandomAccess interface.

    The subList's set(int, Object), get(int), add(int, Object), remove(int), addAll(int, Collection) and removeRange(int, int) methods first check the bounds, adjust offsets and then call the corresponding methods of the original AbstractList. addAll(Collection c) method of the returned subList calls the original addAll(offset + size, c).

    The listIterator(int) method of the subList wraps the original list iterator. The iterator() method of the subList invokes the original listIterator() method, and the size() method merely returns the size of the subList.

    All methods will throw a ConcurrentModificationException if the modCount of the original list is not equal to the expected value.

    ###### Parameters

    - `start`: start index of the subList (inclusive).

    - `end`: end index of the subList, (exclusive).

    ###### Returns

    Specified by:
      `subList` in interface `List<E>`
    Returns:

      a subList view of this list starting from `start` (inclusive), and ending with `end` (exclusive)

      ###### Throws

      - `IndexOutOfBoundsException`: if (start size())

      - `IllegalArgumentException`: if (start > end)

  - ### toArray

    public java.lang.Object[] toArray()

    Returns a new array containing all elements contained in this `ArrayList`.

    ###### Returns

    an array of the elements from this `ArrayList`

    Specified by:
      `toArray` in interface `Collection<E>`
    Specified by:
      `toArray` in interface `List<E>`
    Overrides:
      `toArray` in class `AbstractCollection<E>`

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

    ###### Parameters

    - `contents`: the array.

    ###### Returns

    an array of the elements from this `ArrayList`.

    ###### Throws

    - `ArrayStoreException`: @throws ArrayStoreException when the type of an element in this `ArrayList` cannot be stored in the type of the specified array.
    Specified by:
      `toArray` in interface `Collection<E>`
    Specified by:
      `toArray` in interface `List<E>`
    Overrides:
      `toArray` in class `AbstractCollection<E>`