Class IntList


public final class IntList extends MutabilityControl
Simple list of ints.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final IntList
    non-null; immutable, no-element instance
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an empty instance with a default initial capacity.
    IntList(int initialCapacity)
    Constructs an empty instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(int value)
    Adds an element to the end of the list.
    int
    binarysearch(int value)
    Performs a binary search on a sorted list, returning the index of the given value if it is present or (-(insertion point) - 1) if the value is not present.
    boolean
    contains(int value)
    Returns whether or not the given value appears in the list.
    boolean
    equals(Object other)
    int
    get(int n)
    Gets the indicated value.
    int
    int
    indexOf(int value)
    Returns the index of the given value, or -1 if the value does not appear in the list.
    void
    insert(int n, int value)
    Inserts element into specified index, moving elements at and above that index up one.
    static IntList
    makeImmutable(int value)
    Constructs a new immutable instance with the given element.
    static IntList
    makeImmutable(int value0, int value1)
    Constructs a new immutable instance with the given elements.
    Makes and returns a mutable copy of the list.
    int
    pop()
    Pops an element off the end of the list and decreasing the size by one.
    void
    pop(int n)
    Pops N elements off the end of the list and decreasing the size by N.
    void
    removeIndex(int n)
    Removes an element at a given index, shifting elements at greater indicies down one.
    void
    set(int n, int value)
    Sets the value at the given index.
    void
    shrink(int newSize)
    Shrinks the size of the list.
    int
    Gets the number of elements in this list.
    void
    Sorts the elements in the list in-place.
    int
    top()
    Returns the last element in the array without modifying the array

    Methods inherited from class com.android.dx.util.MutabilityControl

    isImmutable, isMutable, setImmutable, throwIfImmutable, throwIfMutable

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • EMPTY

      public static final IntList EMPTY
      non-null; immutable, no-element instance
  • Constructor Details

    • IntList

      public IntList()
      Constructs an empty instance with a default initial capacity.
    • IntList

      public IntList(int initialCapacity)
      Constructs an empty instance.
      Parameters:
      initialCapacity - >= 0; initial capacity of the list
  • Method Details

    • makeImmutable

      public static IntList makeImmutable(int value)
      Constructs a new immutable instance with the given element.
      Parameters:
      value - the sole value in the list
    • makeImmutable

      public static IntList makeImmutable(int value0, int value1)
      Constructs a new immutable instance with the given elements.
      Parameters:
      value0 - the first value in the list
      value1 - the second value in the list
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • size

      public int size()
      Gets the number of elements in this list.
    • get

      public int get(int n)
      Gets the indicated value.
      Parameters:
      n - >= 0, < size(); which element
      Returns:
      the indicated element's value
    • set

      public void set(int n, int value)
      Sets the value at the given index.
      Parameters:
      n - >= 0, < size(); which element
      value - value to store
    • add

      public void add(int value)
      Adds an element to the end of the list. This will increase the list's capacity if necessary.
      Parameters:
      value - the value to add
    • insert

      public void insert(int n, int value)
      Inserts element into specified index, moving elements at and above that index up one. May not be used to insert at an index beyond the current size (that is, insertion as a last element is legal but no further).
      Parameters:
      n - >= 0, <=size(); index of where to insert
      value - value to insert
    • removeIndex

      public void removeIndex(int n)
      Removes an element at a given index, shifting elements at greater indicies down one.
      Parameters:
      n - >=0, < size(); index of element to remove
    • top

      public int top()
      Returns the last element in the array without modifying the array
      Returns:
      last value in the array
      Throws:
      IndexOutOfBoundsException - if stack is empty
    • pop

      public int pop()
      Pops an element off the end of the list and decreasing the size by one.
      Returns:
      value from what was the last element
      Throws:
      IndexOutOfBoundsException - if stack is empty
    • pop

      public void pop(int n)
      Pops N elements off the end of the list and decreasing the size by N.
      Parameters:
      n - >= 0; number of elements to remove from end
      Throws:
      IndexOutOfBoundsException - if stack is smaller than N
    • shrink

      public void shrink(int newSize)
      Shrinks the size of the list.
      Parameters:
      newSize - >= 0; the new size
    • mutableCopy

      public IntList mutableCopy()
      Makes and returns a mutable copy of the list.
      Returns:
      non-null; an appropriately-constructed instance
    • sort

      public void sort()
      Sorts the elements in the list in-place.
    • indexOf

      public int indexOf(int value)
      Returns the index of the given value, or -1 if the value does not appear in the list. This will do a binary search if the list is sorted or a linear search if not.
      Parameters:
      value - value to find
      Returns:
      index of value or -1
    • binarysearch

      public int binarysearch(int value)
      Performs a binary search on a sorted list, returning the index of the given value if it is present or (-(insertion point) - 1) if the value is not present. If the list is not sorted, then reverts to linear search and returns -size() if the element is not found.
      Parameters:
      value - value to find
      Returns:
      index of value or (-(insertion point) - 1) if the value is not present
    • contains

      public boolean contains(int value)
      Returns whether or not the given value appears in the list. This will do a binary search if the list is sorted or a linear search if not.
      Parameters:
      value - value to look for
      Returns:
      whether the list contains the given value
      See Also: