Interface SortedList<T>

  • All Superinterfaces:
    java.lang.Iterable<T>

    public interface SortedList<T>
    extends java.lang.Iterable<T>
    SortedList interface adapted from Carrano and Henry's interface in Data Structures and Abstractions with Java. A sorted list ADT is like a list, but maintains its entries in sorted order (rather than in the order they were inserted or the order that the user specified).
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      void add​(T item)
      Adds item to the list in sorted order.
      void clear()
      Removes all items from the list.
      boolean contains​(T targetItem)
      Returns true if the list contains the target item.
      T get​(int position)
      Returns the item at a given index.
      int getPosition​(T targetItem)
      Returns the first position of targetItem in the list.
      boolean isEmpty()
      Returns true if the list has no items stored in it.
      java.util.Iterator<T> iterator()
      Returns an iterator that begins just before index 0 in this list.
      T remove​(int position)
      Remove the item at index position from the list, shifting everything after it up one position.
      boolean remove​(T targetItem)
      Remove the first occurence of targetItem from the list, shifting everything after it up one position.
      void resort​(java.util.Comparator<T> comparator)
      Re-sorts the list according to the given comparator.
      int size()
      Returns the length of the list: the number of items stored in it.
      java.lang.Object[] toArray()
      Returns an array version of the list.
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Method Detail

      • add

        void add​(T item)
        Adds item to the list in sorted order.
      • remove

        boolean remove​(T targetItem)
        Remove the first occurence of targetItem from the list, shifting everything after it up one position. targetItem is considered to be in the list if an item that is equal to it (using .equals) is in the list. (This convention for something being in the list should be followed throughout.)
        Returns:
        true if the item was in the list, false otherwise
      • remove

        T remove​(int position)
        Remove the item at index position from the list, shifting everything after it up one position.
        Returns:
        the item, or throw an IndexOutOfBoundsException if the index is out of bounds.
      • getPosition

        int getPosition​(T targetItem)
        Returns the first position of targetItem in the list.
        Returns:
        the position of the item, or -1 if targetItem is not in the list
      • get

        T get​(int position)
        Returns the item at a given index.
        Returns:
        the item, or throw an IndexOutOfBoundsException if the index is out of bounds.
      • contains

        boolean contains​(T targetItem)
        Returns true if the list contains the target item.
      • resort

        void resort​(java.util.Comparator<T> comparator)
        Re-sorts the list according to the given comparator. All future insertions should add in the order specified by this comparator.
      • size

        int size()
        Returns the length of the list: the number of items stored in it.
      • isEmpty

        boolean isEmpty()
        Returns true if the list has no items stored in it.
      • toArray

        java.lang.Object[] toArray()
        Returns an array version of the list. Note that, for technical reasons, the type of the items contained in the list can't be communicated properly to the caller, so an array of Objects gets returned.
        Returns:
        an array of length length(), with the same items in it as are stored in the list, in the same order.
      • iterator

        java.util.Iterator<T> iterator()
        Returns an iterator that begins just before index 0 in this list.
        Specified by:
        iterator in interface java.lang.Iterable<T>
      • clear

        void clear()
        Removes all items from the list.