Package examples

Interface List<E>


public interface List<E>
An ordered, indexed list.

List element positions are indexed starting from 0. Negative positions may also be used, in which case the value will be interpreted as an offset from the end of the list. For example, the final element in any non-empty list may be accessed at the index -1.

  • Method Summary

    Modifier and Type Method Description
    void add​(E element)
    Add the given element to the front of this list.
    void add​(E element, int index)
    Inserts an element at the given position.
    void clear()
    Remove all elements from this collection.
    List<E> cut​(int index)
    Splits the list in two at the given position.
    E first()
    Get the element stored at the beginning of the list.
    E get​(int index)
    Get the element stored at the given position in the list.
    int indexOf​(E element)
    Get the position of the first occurrence of the given element.
    E last()
    Get the element stored at the end of list.
    E remove​(int index)
    Removes the element at the given position from the list and returns it.
    void reverse()
    Reverses the order of all elements in this list.
    int size()
    Returns the number of elements in the list.
  • Method Details

    • size

      int size()
      Returns the number of elements in the list.
      Returns:
      total number of elements stored in this collection.
    • clear

      void clear()
      Remove all elements from this collection.
    • first

      E first() throws NoSuchElementException
      Get the element stored at the beginning of the list. This element is not removed from the collection.
      Returns:
      the first element stored in this collection.
      Throws:
      NoSuchElementException - if the list is empty.
    • last

      E last() throws NoSuchElementException
      Get the element stored at the end of list. This element is not removed from the collection.
      Returns:
      the last element stored in this collection.
      Throws:
      NoSuchElementException - if the list is empty
    • get

      E get​(int index) throws IndexOutOfBoundsException
      Get the element stored at the given position in the list.
      Parameters:
      index - position of the element to retrieve
      Returns:
      the element at the given position
      Throws:
      IndexOutOfBoundsException - if index ∉ [-size(), size())
    • add

      void add​(E element)
      Add the given element to the front of this list.
      Parameters:
      element - element to be added
    • add

      void add​(E element, int index) throws IndexOutOfBoundsException
      Inserts an element at the given position.
      Parameters:
      element - element to be added
      index - position at which the new element will be inserted
      Throws:
      IndexOutOfBoundsException - if index ∉ [-size(), size()]
    • remove

      E remove​(int index) throws IndexOutOfBoundsException
      Removes the element at the given position from the list and returns it.
      Parameters:
      index - position of the element to be removed
      Returns:
      the element that was removed
      Throws:
      IndexOutOfBoundsException - if index ∉ [-size(), size())
    • indexOf

      int indexOf​(E element) throws NoSuchElementException
      Get the position of the first occurrence of the given element. Elements in the collection will be compared to the argument using Object.equals(Object).
      Parameters:
      element - the element to search for
      Returns:
      position index
      Throws:
      NoSuchElementException - if the given element is not stored in this collection.
    • reverse

      void reverse()
      Reverses the order of all elements in this list.
    • cut

      List<E> cut​(int index) throws IndexOutOfBoundsException
      Splits the list in two at the given position. The element at that position and all following elements will be removed from this list and returned as part of a new list.
      Parameters:
      index - position of the first element to be removed
      Returns:
      a new list containing all elements that were removed
      Throws:
      IndexOutOfBoundsException - if index ∉ [-size(), size())