Package cs2110

Interface IndexedSeq<T>

All Superinterfaces:
Iterable<T>
All Known Implementing Classes:
DynamicArrayIndexedSeq, JavaIndexedSeq

public interface IndexedSeq<T> extends Iterable<T>
A sequence of elements of type `T`, which can be accessed via their 0-based index. This is a simpler alternative to Java's `List` interface.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static class 
    An iterator over an indexed sequence of elements of type `T`.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(T value)
    Append `value` to the end of this sequence.
    static boolean
    Return whether `a` and `b` have the same elements.
    get(int index)
    Return the element at index `index`.
    default T
    Return the last element of this sequence.
    static int
    Return a hash code derived from the hash codes of all of the elements in `seq`, dependent on their ordering.
    default boolean
    Return whether this sequence currently contains no elements.
    default Iterator<T>
     
    default T
    Remove and return the last element of this sequence.
    void
    set(int index, T value)
    Assign `value` to the element at index `index`.
    int
    Return the number of elements in this sequence.
    default <U extends Comparable<? super U>>
    void
    Sort the distinct elements in this sequence in ascending order, discarding duplicate copies.
    default void
    swap(int i, int j)
    Swap the elements at indices `i` and `j`.
    static <T> String
    Return a String representation of `seq`.
    void
    truncate(int end)
    Truncate this sequence, retaining only the elements with indices `[0..end)`.

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Method Details

    • get

      T get(int index)
      Return the element at index `index`. Throws `IndexOutOfBoundsException` if `index` is negative or is not less than this sequence's size.
    • set

      void set(int index, T value)
      Assign `value` to the element at index `index`. Throws `IndexOutOfBoundsException` if `index` is negative or is not less than this sequence's size. Requires `value` is not null.
    • size

      int size()
      Return the number of elements in this sequence.
    • add

      void add(T value)
      Append `value` to the end of this sequence. Requires `value` is not null.
    • truncate

      void truncate(int end)
      Truncate this sequence, retaining only the elements with indices `[0..end)`.
    • isEmpty

      default boolean isEmpty()
      Return whether this sequence currently contains no elements.
    • getLast

      default T getLast()
      Return the last element of this sequence. Throws `NoSuchElementException` if this sequence is empty.
    • removeLast

      default T removeLast()
      Remove and return the last element of this sequence. Throws `NoSuchElementException` if this sequence is empty.
    • swap

      default void swap(int i, int j)
      Swap the elements at indices `i` and `j`. Throws `IndexOutOfBoundsException` if `i` or `j` is negative or is not less than this list's size.
    • sortDistinct

      default <U extends Comparable<? super U>> void sortDistinct()
      Sort the distinct elements in this sequence in ascending order, discarding duplicate copies. Requires `T` is `Comparable` to itself (that is, `T` satisfies the bounds on `U`).
    • iterator

      default Iterator<T> iterator()
      Specified by:
      iterator in interface Iterable<T>
    • equals

      static boolean equals(IndexedSeq<?> a, IndexedSeq<?> b)
      Return whether `a` and `b` have the same elements. That is, every element in `a` is equal to the corresponding element in `b`, and `b` has no additional elements beyond those in `a`. The result is agnostic to the dynamic types of `a` and `b`.

      Implementations may choose to call this when overriding their own `equals()`.

    • hashCode

      static int hashCode(IndexedSeq<?> seq)
      Return a hash code derived from the hash codes of all of the elements in `seq`, dependent on their ordering. Implementations may choose to call this when overriding their own `hashCode()`.
    • toString

      static <T> String toString(IndexedSeq<T> seq)
      Return a String representation of `seq`.