Class IntegerList

java.lang.Object
org.cicirello.util.IntegerList
All Implemented Interfaces:
Copyable<IntegerList>

public final class IntegerList extends Object implements Copyable<IntegerList>
This class is an implementation of a partially-filled array of primitive int values.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The default initial capacity of the list.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Initializes an empty IntegerList.
    IntegerList(int initialCapacity)
    Initializes an empty IntegerList, with a specified initial capacity.
    IntegerList(int[] initialContents)
    Initializes an IntegerList from an array,
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(int element)
    Adds a new element to the end of the list, growing the internal array if necessary.
    void
    add(int index, int element)
    Inserts a new element into the list, growing the internal array if necessary.
    void
    Logically clears the list.
    boolean
    contains(int element)
    Checks if the list contains a value.
    Creates an identical copy of this object.
    void
    ensureCapacity(int minCapacity)
    Increases capacity if necessary to achieve a specified minimum capacity.
    boolean
    equals(Object other)
    Checks if this list is equal to another list, such that they are the same size, and contain all of the same elements in the same order.
    int
    get(int index)
    Gets the element at a specified index.
    int
    Computes a hashCode for the list.
    int
    indexOf(int element)
    Gets the index of the first occurrence of an element from the left.
    boolean
    Checks if the list is empty.
    int
    lastIndexOf(int element)
    Gets the index of the last occurrence of an element.
    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 list.
    void
    Sorts list into ascending order.
    int[]
    Returns an array containing all of the element from the list.
    Generate a String representation of the list.
    void
    Reduces the capacity of the list to the current value of size().

    Methods inherited from class java.lang.Object

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

    • DEFAULT_INITIAL_CAPACITY

      public static final int DEFAULT_INITIAL_CAPACITY
      The default initial capacity of the list. The capacity will grow as needed.
      See Also:
  • Constructor Details

    • IntegerList

      public IntegerList()
      Initializes an empty IntegerList.
    • IntegerList

      public IntegerList(int initialCapacity)
      Initializes an empty IntegerList, with a specified initial capacity. This may be useful if you anticipate adding a large number of elements as it can help minimize the number of array reallocations.
      Parameters:
      initialCapacity - The initial capacity for the buffer array holding the contents.
      Throws:
      IllegalArgumentException - if initialCapacity is less than 1.
    • IntegerList

      public IntegerList(int[] initialContents)
      Initializes an IntegerList from an array,
      Parameters:
      initialContents - The initial contents of the list, which must contain at least one element. This constructor clones this array so it is free of unintended side-effects.
      Throws:
      IllegalArgumentException - if initialContents.length is less than one.
  • Method Details

    • add

      public void add(int element)
      Adds a new element to the end of the list, growing the internal array if necessary.
      Parameters:
      element - The value to add.
    • add

      public void add(int index, int element)
      Inserts a new element into the list, growing the internal array if necessary.
      Parameters:
      index - The index for the insertion.
      element - The value to add.
      Throws:
      IndexOutOfBoundsException - if the index is out of bounds, such that index is negative or index is greater than size().
    • clear

      public void clear()
      Logically clears the list.
    • contains

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

      public IntegerList copy()
      Description copied from interface: Copyable
      Creates an identical copy of this object.
      Specified by:
      copy in interface Copyable<IntegerList>
      Returns:
      an identical copy of this object.
    • ensureCapacity

      public void ensureCapacity(int minCapacity)
      Increases capacity if necessary to achieve a specified minimum capacity. If increased, the new capacity may or may not exactly equal to requested minimum. The only guarantee is that it is at least as large as requested.
      Parameters:
      minCapacity - The minimum capacity for the list.
    • 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.
      Parameters:
      element - The element searched for.
      Returns:
      The index of the first occurrence of the element in the list from the left, or -1 if the element doesn't exist.
    • isEmpty

      public boolean isEmpty()
      Checks if the list is 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.
      Parameters:
      element - The element searched for.
      Returns:
      The index of the last occurrence of the element in the list, or -1 if the element doesn't exist.
    • 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 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 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 list.
      Returns:
      The size of the list, which is the number of elements currently within it.
    • sort

      public void sort()
      Sorts list into ascending order.
    • toArray

      public int[] toArray()
      Returns an array containing all of the element from the list. This method constructs a new array with length equal to the current size() of the list. This method does not expose the encapsulated array.
      Returns:
      An array containing all of the elements currently in the list.
    • trimToSize

      public void trimToSize()
      Reduces the capacity of the list to the current value of size(). However, if size() is 0, this method will trim the list capacity to 1. If you don't anticipate adding additional elements to the list, and if your application is memory constrained, then this method can save memory.
    • equals

      public boolean equals(Object other)
      Checks if this list is equal to another list, such that they are the same size, and contain all of the same elements in the same order.
      Overrides:
      equals in class Object
      Parameters:
      other - The other list
      Returns:
      true If the lists are the same size and contain the same elements in the same order.
    • hashCode

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

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