Package org.fife.util

Class DynamicIntArray

java.lang.Object
org.fife.util.DynamicIntArray
All Implemented Interfaces:
Serializable

public class DynamicIntArray extends Object implements Serializable
Similar to a java.util.ArrayList, but specifically for ints. This is basically an array of integers that resizes itself (if necessary) when adding new elements.
Version:
0.8
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private int[]
    The actual data.
    private int
    The number of values in the array.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new array object with an initial capacity of 10.
    DynamicIntArray(int initialCapacity)
    Constructs a new array object with a given initial capacity.
    DynamicIntArray(int[] intArray)
    Constructs a new array object from the given int array.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(int value)
    Appends the specified int to the end of this array.
    void
    add(int index, int value)
    Inserts the specified int at the specified position in this array.
    void
    add(int index, int[] intArray)
    Inserts all ints in the specified array into this array object at the specified location.
    void
    Removes all values from this array object.
    boolean
    contains(int integer)
    Returns whether this array contains a given integer.
    void
    decrement(int from, int to)
    Decrements all values in the array in the specified range.
    private void
    ensureCapacity(int minCapacity)
    Makes sure that this DynamicIntArray instance can hold at least the number of elements specified.
    void
    fill(int value)
    Sets the value of all entries in this array to the specified value.
    int
    get(int index)
    Returns the int at the specified position in this array object.
    int
    Returns the number of ints in this array object.
    int
    getUnsafe(int index)
    Returns the int at the specified position in this array object, without doing any bounds checking.
    void
    increment(int from, int to)
    Increments all values in the array in the specified range.
    void
    insertRange(int offs, int count, int value)
    Inserts a specific value multiple times into a specific offset in this array.
    boolean
    Returns whether this array object is empty.
    void
    remove(int index)
    Removes the int at the specified location from this array object.
    void
    removeRange(int fromIndex, int toIndex)
    Removes the ints in the specified range from this array object.
    void
    set(int index, int value)
    Sets the int value at the specified position in this array object.
    void
    setUnsafe(int index, int value)
    Sets the int value at the specified position in this array object, without doing any bounds checking.
    private void
    throwException(int index)
    Throws an exception.
    private void
    throwException2(int index)
    Throws an exception.
    private void
    throwException3(int fromIndex, int toIndex)
    Throws an exception.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • data

      private int[] data
      The actual data.
    • size

      private int size
      The number of values in the array. Note that this is NOT the capacity of the array; rather, size <= capacity.
  • Constructor Details

    • DynamicIntArray

      public DynamicIntArray()
      Constructs a new array object with an initial capacity of 10.
    • DynamicIntArray

      public DynamicIntArray(int initialCapacity)
      Constructs a new array object with a given initial capacity.
      Parameters:
      initialCapacity - The initial capacity.
      Throws:
      IllegalArgumentException - If initialCapacity is negative.
    • DynamicIntArray

      public DynamicIntArray(int[] intArray)
      Constructs a new array object from the given int array. The resulting DynamicIntArray will have an initial capacity of 110% the size of the array.
      Parameters:
      intArray - Initial data for the array object.
      Throws:
      NullPointerException - If intArray is null.
  • Method Details

    • add

      public void add(int value)
      Appends the specified int to the end of this array.
      Parameters:
      value - The int to be appended to this array.
    • add

      public void add(int index, int[] intArray)
      Inserts all ints in the specified array into this array object at the specified location. Shifts the int currently at that position (if any) and any subsequent ints to the right (adds one to their indices).
      Parameters:
      index - The index at which the specified integer is to be inserted.
      intArray - The array of ints to insert.
      Throws:
      IndexOutOfBoundsException - If index is less than zero or greater than getSize().
      NullPointerException - If intArray is null.
    • add

      public void add(int index, int value)
      Inserts the specified int at the specified position in this array. Shifts the int currently at that position (if any) and any subsequent ints to the right (adds one to their indices).
      Parameters:
      index - The index at which the specified integer is to be inserted.
      value - The int to be inserted.
      Throws:
      IndexOutOfBoundsException - If index is less than zero or greater than getSize().
    • clear

      public void clear()
      Removes all values from this array object. Capacity will remain the same.
    • contains

      public boolean contains(int integer)
      Returns whether this array contains a given integer. This method performs a linear search, so it is not optimized for performance.
      Parameters:
      integer - The int for which to search.
      Returns:
      Whether the given integer is contained in this array.
    • decrement

      public void decrement(int from, int to)
      Decrements all values in the array in the specified range.
      Parameters:
      from - The range start offset (inclusive).
      to - The range end offset (exclusive).
      See Also:
    • ensureCapacity

      private void ensureCapacity(int minCapacity)
      Makes sure that this DynamicIntArray instance can hold at least the number of elements specified. If it can't, then the capacity is increased.
      Parameters:
      minCapacity - The desired minimum capacity.
    • fill

      public void fill(int value)
      Sets the value of all entries in this array to the specified value.
      Parameters:
      value - The new value for all elements in the array.
    • get

      public int get(int index)
      Returns the int at the specified position in this array object.
      Parameters:
      index - The index of the int to return.
      Returns:
      The int at the specified position in this array.
      Throws:
      IndexOutOfBoundsException - If index is less than zero or greater than or equal to getSize().
    • getUnsafe

      public int getUnsafe(int index)
      Returns the int at the specified position in this array object, without doing any bounds checking. You really should use get(int) instead of this method.
      Parameters:
      index - The index of the int to return.
      Returns:
      The int at the specified position in this array.
    • getSize

      public int getSize()
      Returns the number of ints in this array object.
      Returns:
      The number of ints in this array object.
    • increment

      public void increment(int from, int to)
      Increments all values in the array in the specified range.
      Parameters:
      from - The range start offset (inclusive).
      to - The range end offset (exclusive).
      See Also:
    • insertRange

      public void insertRange(int offs, int count, int value)
      Inserts a specific value multiple times into a specific offset in this array.
      Parameters:
      offs - The offset to insert at.
      count - The number of values to insert.
      value - The value to insert.
    • isEmpty

      public boolean isEmpty()
      Returns whether this array object is empty.
      Returns:
      Whether this array object contains no elements.
    • remove

      public void remove(int index)
      Removes the int at the specified location from this array object.
      Parameters:
      index - The index of the int to remove.
      Throws:
      IndexOutOfBoundsException - If index is less than zero or greater than or equal to getSize().
    • removeRange

      public void removeRange(int fromIndex, int toIndex)
      Removes the ints in the specified range from this array object.
      Parameters:
      fromIndex - The index of the first int to remove.
      toIndex - The index AFTER the last int to remove.
      Throws:
      IndexOutOfBoundsException - If either of fromIndex or toIndex is less than zero or greater than or equal to getSize().
    • set

      public void set(int index, int value)
      Sets the int value at the specified position in this array object.
      Parameters:
      index - The index of the int to set
      value - The value to set it to.
      Throws:
      IndexOutOfBoundsException - If index is less than zero or greater than or equal to getSize().
    • setUnsafe

      public void setUnsafe(int index, int value)
      Sets the int value at the specified position in this array object, without doing any bounds checking. You should use set(int, int) instead of this method.
      Parameters:
      index - The index of the int to set
      value - The value to set it to.
    • throwException

      private void throwException(int index)
      Throws an exception. This method isolates error-handling code from the error-checking code, so that callers (e.g. get(int) and set(int, int)) can be both small enough to be inlined, as well as not usually make any expensive method calls (since their callers will usually not pass illegal arguments to them). See this Sun bug report for more information.
      Parameters:
      index - The invalid index.
      Throws:
      IndexOutOfBoundsException - Always.
    • throwException2

      private void throwException2(int index)
      Throws an exception. This method isolates error-handling code from the error-checking code, so that callers can be both small enough to be inlined, as well as not usually make any expensive method calls (since their callers will usually not pass illegal arguments to them). See this Sun bug report for more information.
      Parameters:
      index - The invalid index.
      Throws:
      IndexOutOfBoundsException - Always.
    • throwException3

      private void throwException3(int fromIndex, int toIndex)
      Throws an exception. This method isolates error-handling code from the error-checking code, so that callers can be both small enough to be inlined, as well as not usually make any expensive method calls (since their callers will usually not pass illegal arguments to them). See this Sun bug report for more information.
      Parameters:
      fromIndex - The from-index.
      toIndex - The to-index.
      Throws:
      IndexOutOfBoundsException - Always.