Package org.fife.util
Class DynamicIntArray
- java.lang.Object
-
- org.fife.util.DynamicIntArray
-
- All Implemented Interfaces:
java.io.Serializable
public class DynamicIntArray extends java.lang.Object implements java.io.SerializableSimilar to ajava.util.ArrayList, but specifically forints. This is basically an array of integers that resizes itself (if necessary) when adding new elements.- Version:
- 0.8
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description DynamicIntArray()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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(int value)Appends the specifiedintto the end of this array.voidadd(int index, int value)Inserts the specifiedintat the specified position in this array.voidadd(int index, int[] intArray)Inserts allints in the specified array into this array object at the specified location.voidclear()Removes all values from this array object.booleancontains(int integer)Returns whether this array contains a given integer.voiddecrement(int from, int to)Decrements all values in the array in the specified range.private voidensureCapacity(int minCapacity)Makes sure that thisDynamicIntArrayinstance can hold at least the number of elements specified.voidfill(int value)Sets the value of all entries in this array to the specified value.intget(int index)Returns theintat the specified position in this array object.intgetSize()Returns the number ofints in this array object.intgetUnsafe(int index)Returns theintat the specified position in this array object, without doing any bounds checking.voidincrement(int from, int to)Increments all values in the array in the specified range.voidinsertRange(int offs, int count, int value)Inserts a specific value multiple times into a specific offset in this array.booleanisEmpty()Returns whether this array object is empty.voidremove(int index)Removes theintat the specified location from this array object.voidremoveRange(int fromIndex, int toIndex)Removes theints in the specified range from this array object.voidset(int index, int value)Sets theintvalue at the specified position in this array object.voidsetUnsafe(int index, int value)Sets theintvalue at the specified position in this array object, without doing any bounds checking.private voidthrowException(int index)Throws an exception.private voidthrowException2(int index)Throws an exception.private voidthrowException3(int fromIndex, int toIndex)Throws an exception.
-
-
-
Constructor Detail
-
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:
java.lang.IllegalArgumentException- IfinitialCapacityis negative.
-
DynamicIntArray
public DynamicIntArray(int[] intArray)
Constructs a new array object from the given int array. The resultingDynamicIntArraywill have an initial capacity of 110% the size of the array.- Parameters:
intArray- Initial data for the array object.- Throws:
java.lang.NullPointerException- IfintArrayisnull.
-
-
Method Detail
-
add
public void add(int value)
Appends the specifiedintto the end of this array.- Parameters:
value- Theintto be appended to this array.
-
add
public void add(int index, int[] intArray)Inserts allints in the specified array into this array object at the specified location. Shifts theintcurrently at that position (if any) and any subsequentints to the right (adds one to their indices).- Parameters:
index- The index at which the specified integer is to be inserted.intArray- The array ofints to insert.- Throws:
java.lang.IndexOutOfBoundsException- Ifindexis less than zero or greater thangetSize().java.lang.NullPointerException- IfintArrayisnull.
-
add
public void add(int index, int value)Inserts the specifiedintat the specified position in this array. Shifts theintcurrently at that position (if any) and any subsequentints to the right (adds one to their indices).- Parameters:
index- The index at which the specified integer is to be inserted.value- Theintto be inserted.- Throws:
java.lang.IndexOutOfBoundsException- Ifindexis less than zero or greater thangetSize().
-
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- Theintfor 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:
increment(int, int)
-
ensureCapacity
private void ensureCapacity(int minCapacity)
Makes sure that thisDynamicIntArrayinstance 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 theintat the specified position in this array object.- Parameters:
index- The index of theintto return.- Returns:
- The
intat the specified position in this array. - Throws:
java.lang.IndexOutOfBoundsException- Ifindexis less than zero or greater than or equal togetSize().
-
getUnsafe
public int getUnsafe(int index)
Returns theintat the specified position in this array object, without doing any bounds checking. You really should useget(int)instead of this method.- Parameters:
index- The index of theintto return.- Returns:
- The
intat the specified position in this array.
-
getSize
public int getSize()
Returns the number ofints 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:
decrement(int, int)
-
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 theintat the specified location from this array object.- Parameters:
index- The index of theintto remove.- Throws:
java.lang.IndexOutOfBoundsException- Ifindexis less than zero or greater than or equal togetSize().
-
removeRange
public void removeRange(int fromIndex, int toIndex)Removes theints in the specified range from this array object.- Parameters:
fromIndex- The index of the firstintto remove.toIndex- The index AFTER the lastintto remove.- Throws:
java.lang.IndexOutOfBoundsException- If either offromIndexortoIndexis less than zero or greater than or equal togetSize().
-
set
public void set(int index, int value)Sets theintvalue at the specified position in this array object.- Parameters:
index- The index of theintto setvalue- The value to set it to.- Throws:
java.lang.IndexOutOfBoundsException- Ifindexis less than zero or greater than or equal togetSize().
-
setUnsafe
public void setUnsafe(int index, int value)Sets theintvalue at the specified position in this array object, without doing any bounds checking. You should useset(int, int)instead of this method.- Parameters:
index- The index of theintto setvalue- 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)andset(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:
java.lang.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:
java.lang.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:
java.lang.IndexOutOfBoundsException- Always.
-
-