Class IntegerArray

java.lang.Object
org.cicirello.util.IntegerArray

public final class IntegerArray extends Object
An object of this class wraps an array of primitive ints in such a way as to provide some ArrayList-like operations for a primitive array (e.g., using it as a partially-filled array). It doesn't support growable operations, instead keeping array length fixed. An instance of this class does not strongly encapsulate the array. If initialized from an existing array, it maintains a reference to that array, and regardless also provides an array method that exposes a reference to the array.

For a strongly encapsulated partially-filled array of primitive ints, including growability, see the IntegerList class.

  • Constructor Summary

    Constructors
    Constructor
    Description
    IntegerArray(int length)
    Initializes an empty IntegerArray, with a specified length for the internal array.
    IntegerArray(int[] array)
    Initializes an IntegerArray so as to wrap an existing array.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(int element)
    Adds a new element, provided size() is less than length(); and otherwise throws an exception.
    void
    add(int index, int element)
    Inserts a new element at a specified index, provided size() is less than length(); and otherwise throws an exception.
    int[]
    Exposes a reference to the wrapped array.
    void
    Logically clears the array.
    boolean
    contains(int element)
    Checks if the array contains a value within the first size() elements.
    boolean
    equals(Object other)
    Checks if this IntegerArray is equal to another IntegerArray, such that they wrap the same array instance and are of the same size().
    int
    get(int index)
    Gets the element at a specified index.
    int
    Computes a hashCode for the IntegerArray.
    int
    indexOf(int element)
    Gets the index of the first occurrence of an element from the left, provided it appears within the first size() elements.
    boolean
    Checks if the array is logically empty.
    int
    lastIndexOf(int element)
    Gets the index of the last occurrence of an element, provided it appears within the first size() elements.
    int
    Gets the length of the wrapped array.
    int
    remove(int index)
    Removes the element at a specified index.
    void
    set(int index, int element)
    Sets an element at a specified index, replacing any value that is currently there.
    int
    Gets the size of the array (i.e., the number of elements added via the add(int) methods).
    void
    Sorts the first size() elements of the array into ascending order.
    Generate a String representation of the IntegerArray.

    Methods inherited from class java.lang.Object

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

    • IntegerArray

      public IntegerArray(int length)
      Initializes an empty IntegerArray, with a specified length for the internal array. By empty, we mean size() equals 0.
      Parameters:
      length - the length of the array within the IntegerArray
      Throws:
      NegativeArraySizeException - if length is negative
    • IntegerArray

      public IntegerArray(int[] array)
      Initializes an IntegerArray so as to wrap an existing array. A reference to this array is stored in the IntegerArray object, and thus changes to the array externally are observable within the IntegerArray; and likewise changes via the methods of this class are observable directly via the array reference.

      The size() of this IntegerArray is 0, and doesn't depend upon the length of array. However, the length() of this IntegerArray equals array.length.

      Parameters:
      array - the array to wrap
  • Method Details

    • add

      public void add(int element)
      Adds a new element, provided size() is less than length(); and otherwise throws an exception.
      Parameters:
      element - The value to add.
      Throws:
      IndexOutOfBoundsException - if size() is equal to length()
    • add

      public void add(int index, int element)
      Inserts a new element at a specified index, provided size() is less than length(); and otherwise throws an exception.
      Parameters:
      index - The index for the insertion.
      element - The value to add.
      Throws:
      IndexOutOfBoundsException - if size() is equal to length() or if index is greater than size()
    • array

      public int[] array()
      Exposes a reference to the wrapped array.
      Returns:
      a reference to the wrapped array
    • clear

      public void clear()
      Logically clears the array. That is, sets size() to 0, but the contents of the array is otherise untouched.
    • contains

      public boolean contains(int element)
      Checks if the array contains a value within the first size() elements.
      Parameters:
      element - The element to search for.
      Returns:
      true if and only if the array contains at least one copy of element within the first size() elements.
    • get

      public int get(int index)
      Gets the element at a specified index.
      Parameters:
      index - The index of the desired element.
      Returns:
      The element at the specified index.
      Throws:
      IndexOutOfBoundsException - if the index is out of bounds, such that index is negative or index is greater than or equal to size().
    • indexOf

      public int indexOf(int element)
      Gets the index of the first occurrence of an element from the left, provided it appears within the first size() elements.
      Parameters:
      element - The element searched for.
      Returns:
      The index of the first occurrence of the element in the array from the left, or -1 if the element doesn't exist within the first size() elements.
    • isEmpty

      public boolean isEmpty()
      Checks if the array is logically empty.
      Returns:
      true if and only if size() equals 0.
    • lastIndexOf

      public int lastIndexOf(int element)
      Gets the index of the last occurrence of an element, provided it appears within the first size() elements.
      Parameters:
      element - The element searched for.
      Returns:
      The index of the last occurrence of the element in the array within the first size() elements, or -1 if the element doesn't exist.
    • length

      public int length()
      Gets the length of the wrapped array.
      Returns:
      the length of the wrapped array
    • remove

      public int remove(int index)
      Removes the element at a specified index.
      Parameters:
      index - The index of the element to remove.
      Returns:
      The element that was removed.
      Throws:
      IndexOutOfBoundsException - if the index is logically out of bounds, such that index is negative or index is greater than or equal to size().
    • set

      public void set(int index, int element)
      Sets an element at a specified index, replacing any value that is currently there.
      Parameters:
      index - The index.
      element - The new element to set at that index.
      Throws:
      IndexOutOfBoundsException - if the index is logically out of bounds, such that index is negative or index is greater than or equal to size().
    • size

      public int size()
      Gets the size of the array (i.e., the number of elements added via the add(int) methods).
      Returns:
      The size of the array, which is the number of elements currently within it.
    • sort

      public void sort()
      Sorts the first size() elements of the array into ascending order.
    • equals

      public boolean equals(Object other)
      Checks if this IntegerArray is equal to another IntegerArray, such that they wrap the same array instance and are of the same size().
      Overrides:
      equals in class Object
      Parameters:
      other - The other IntegerArray
      Returns:
      true if of the same size() and wraps the same array instances.
    • hashCode

      public int hashCode()
      Computes a hashCode for the IntegerArray.
      Overrides:
      hashCode in class Object
      Returns:
      a hashCode
    • toString

      public String toString()
      Generate a String representation of the IntegerArray.
      Overrides:
      toString in class Object
      Returns:
      A String representation of the IntegerArray.