Class SortedArrayList<E>

All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess

public class SortedArrayList<E> extends ArrayList<E>
A SortedArrayList stores its elements in hashCode order and provides means of quickly locating objects.
Author:
AO Industries, Inc.
See Also:
  • Constructor Details

    • SortedArrayList

      public SortedArrayList(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
    • SortedArrayList

      public SortedArrayList()
      Constructs an empty list with an initial capacity of ten.
  • Method Details

    • binarySearchHashCode

      protected int binarySearchHashCode(int elemHash)
      Performs a binary search on hashCode values only. It will return any matching element, not necessarily the first or the last.
    • indexOf

      public int indexOf(Object elem)
      Searches for the first occurrence of the given argument, testing for equality using the equals method.
      Specified by:
      indexOf in interface List<E>
      Overrides:
      indexOf in class ArrayList<E>
      Parameters:
      elem - an object.
      Returns:
      the index of the first occurrence of the argument in this list; returns -1 if the object is not found.
      See Also:
    • indexOf

      public int indexOf(int hashCode)
      Finds the first index where the object has the provided hashCode.
    • lastIndexOf

      public int lastIndexOf(Object elem)
      Returns the index of the last occurrence of the specified object in this list.
      Specified by:
      lastIndexOf in interface List<E>
      Overrides:
      lastIndexOf in class ArrayList<E>
      Parameters:
      elem - the desired element.
      Returns:
      the index of the last occurrence of the specified object in this list; returns -1 if the object is not found.
    • set

      public E set(int index, E element)
      Not allowed to set specific indexes.
      Specified by:
      set in interface List<E>
      Overrides:
      set in class ArrayList<E>
    • add

      public boolean add(E o)
      Adds the specified element in sorted position within this list. When two elements have the same hashCode, the new item is added at the end of the list of matching hashCodes.
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface List<E>
      Overrides:
      add in class ArrayList<E>
      Parameters:
      o - element to be appended to this list.
      Returns:
      true (as per the general contract of Collection.add).
    • add

      public void add(int index, E element)
      Not allowed to add to specific indexes.
      Specified by:
      add in interface List<E>
      Overrides:
      add in class ArrayList<E>
    • remove

      public boolean remove(Object o)
      Removes a single instance of the specified element from this list, if it is present (optional operation). If the list contains one or more such elements. Returns true if the list contained the specified element (or equivalently, if the list changed as a result of the call).
      Specified by:
      remove in interface Collection<E>
      Specified by:
      remove in interface List<E>
      Overrides:
      remove in class ArrayList<E>
      Parameters:
      o - element to be removed from this list, if present.
      Returns:
      true if the list contained the specified element.
    • addAll

      public boolean addAll(Collection<? extends E> c)
      Adds all of the elements in the specified Collection and sorts during the add. This may operate slowly as it is the same as individual calls to the add method.
      Specified by:
      addAll in interface Collection<E>
      Specified by:
      addAll in interface List<E>
      Overrides:
      addAll in class ArrayList<E>
    • addAll

      public boolean addAll(int index, Collection<? extends E> c)
      Not allowed to add to a specific index.
      Specified by:
      addAll in interface List<E>
      Overrides:
      addAll in class ArrayList<E>