Subclasses will want to provide the following methods (which are not declared in this class since subclasses should specify the explicit return type):
<type> get(int)void set(int, <type>)void add(<type>)void add(int, <type>)<type>[] toArray()Clone cannot be supported since the array is not held locally. But the @link #AbstractArray(AbstractArray) constructor can be used for suclasses that need to support clone.
This "type-specific collections" approach was originally developed by Dennis Sosnoski, who provides a more complete library at the referenced URL. Sosnoski's library does not integrate with the jdk collection classes but provides collection-like classes.
| Modifier and Type | Field and Description |
|---|---|
| protected int | capacity
Size of the current array, which can be larger than the
|
| protected int | modCountIncr
The modification count increment indicates if a structural change occurred as a result of an operation that would make concurrent iteration over the array invalid. |
| protected int | size
The number of values currently present in the array. |
| Access | Constructor and Description |
|---|---|
| public | AbstractArray(AbstractArray toCopy)
Since AbstractArray can support a clone method, this facilitates subclasses that want to implement clone (poor man's cloning). |
| public | |
| public | AbstractArray(Class<T>
array element type (primitive type or object class) type)Creates the managed array with a default size of 10. |
| public | AbstractArray(Class<T>
Array element type (primitive type or object class). type, int[] An int array specifying the dimensions. For
a 2D array, something like dimensions)new int[] {10,0} to
create 10 elements each of which can hold an reference to an
array of the same type.Construtor for multi-dimensional array types. |
| public | AbstractArray(Class<T>
array element type (primitive type or object class) type, int number of elements initially allowed in array size)Creates the managed array with the specified size. |
| Modifier and Type | Method and Description |
|---|---|
| public void | appendArray(Object
the array to append ofArrayType)Appends the supplied array, which must be an array of the same
type as |
| public void | clear()
Set the array to the empty state, clearing all the data out and nulling objects (or "zero-ing" primitives). |
| protected void | clearRange(int
the start index, inclusive start, int the stop index, exclusive stop)Clears out the values in the specified range. |
| private void | clearRangeInternal(int
the start index, inclusive start, int the stop index, exclusive stop)Used internally, no bounds checking. |
| public Object | Returns: array containing a shallow copy of the data.Constructs and returns a simple array containing the same data as held in this growable array. |
| protected abstract Object | |
| protected void | ensureCapacity(int
new minimum size required minCapacity)Ensures that the base array has at least the specified minimum capacity. |
| protected int | Returns: index position for next added elementGets the next add position for appending a value to those in the array. |
| protected abstract Object | |
| public int | Returns: the modification count increment (0 if no change, 1 if changed)Returns the modification count increment, which is used by
|
| public int | |
| protected boolean | |
| protected void | makeInsertSpace(int
index position at which to insert element index)Makes room to insert a value at a specified index in the array. |
| protected void | |
| public void | |
| public void | remove(int
inclusive start, int exclusive stop)Removes a range from the array at the specified indices. |
| public void | |
| public void | replaceSubArray(int
the start index (inclusive) of the subarray in this
array to be replaced thisStart, int the stop index (exclusive) of the subarray in this
array to be replaced thisStop, Object the source array from which to copy srcArray, int the start index (inclusive) of the replacement subarray srcStart, int the stop index (exclusive) of the replacement subarray srcStop)Replace a range of this array with another subarray. |
| protected abstract void | |
| private void | setNewBase(int newCapacity)
Replaces the existing base array in the subclass with a new base array resized to the specified capacity. |
| public void | setSize(int
number of values to be set count)Sets the number of values currently present in the array. |
| public String | toString()
Overrides java. Provides a default comma-delimited representation of array. |
| protected void | trimToSize()
Removes any excess capacity in the backing array so it is just big enough to hold the amount of data actually in the array. |
| capacity | back to summary |
|---|---|
| protected int capacity Size of the current array, which can be larger than the
| |
| modCountIncr | back to summary |
|---|---|
| protected int modCountIncr The modification count increment indicates if a structural change
occurred as a result of an operation that would make concurrent iteration
over the array invalid. It is typically used by subclasses that
extend
This class uses a somewhat stricter semantic for | |
| size | back to summary |
|---|---|
| protected int size The number of values currently present in the array. | |
| AbstractArray | back to summary |
|---|---|
| public AbstractArray(AbstractArray toCopy) Since AbstractArray can support a clone method, this facilitates subclasses that want to implement clone (poor man's cloning). Subclasses can then do this: public MyManagedArray(MyManagedArray toCopy) {
super(this);
this.baseArray = (<my array type>) toCopy.copyArray();
this.someProp = toCopy.someProp;
<etc>
}
public Object clone() {
return new MyManagedArray(this);
}
| |
| AbstractArray | back to summary |
|---|---|
| public AbstractArray(int size) Use when the subclass has a preexisting array.
| |
| AbstractArray | back to summary |
|---|---|
| public AbstractArray(Class<T> type) Creates the managed array with a default size of 10.
| |
| AbstractArray | back to summary |
|---|---|
| public AbstractArray(Class<T> type, int[] dimensions) Construtor for multi-dimensional array types.
For example,
Array#newInstance(java.
| |
| AbstractArray | back to summary |
|---|---|
| public AbstractArray(Class<T> type, int size) Creates the managed array with the specified size.
| |
| appendArray | back to summary |
|---|---|
| public void appendArray(Object ofArrayType) Appends the supplied array, which must be an array of the same
type as
| |
| clear | back to summary |
|---|---|
| public void clear() Set the array to the empty state, clearing all the data out and nulling objects (or "zero-ing" primitives). Note This method does not set
| |
| clearRange | back to summary |
|---|---|
| protected void clearRange(int start, int stop) Clears out the values in the specified range. For object arrays, the cleared range is nullified. For primitve arrays, it is "zero-ed" out. Note This method does not set
| |
| clearRangeInternal | back to summary |
|---|---|
| private void clearRangeInternal(int start, int stop) Used internally, no bounds checking.
| |
| copyArray | back to summary |
|---|---|
| public Object copyArray() Constructs and returns a simple array containing the same data as held in this growable array.
| |
| createArray | back to summary |
|---|---|
protected abstract Object createArray(int size)
| |
| ensureCapacity | back to summary |
|---|---|
| protected void ensureCapacity(int minCapacity) Ensures that the base array has at least the specified minimum capacity.
| |
| getAddIndex | back to summary |
|---|---|
| protected int getAddIndex() Gets the next add position for appending a value to those in the array. If the underlying array is full, it is grown by the appropriate size increment so that the index value returned is always valid for the array in use by the time of the return.
| |
| getArray | back to summary |
|---|---|
| protected abstract Object getArray() Get the backing array. This method is used by the type-agnostic base class code to access the array used for type-specific storage by the child class.
| |
| getModCountIncr | back to summary |
|---|---|
| public int getModCountIncr() Returns the modification count increment, which is used by
| |
| getSize | back to summary |
|---|---|
| public int getSize() Get the number of values currently present in the array.
| |
| isEmpty | back to summary |
|---|---|
| protected boolean isEmpty() | |
| makeInsertSpace | back to summary |
|---|---|
| protected void makeInsertSpace(int index) Makes room to insert a value at a specified index in the array.
| |
| makeInsertSpace | back to summary |
|---|---|
| protected void makeInsertSpace(int index, int length) | |
| remove | back to summary |
|---|---|
| public void remove(int index) Remove a value from the array. All values above the index removed are moved down one index position.
| |
| remove | back to summary |
|---|---|
| public void remove(int start, int stop) Removes a range from the array at the specified indices.
| |
| replaceSubArray | back to summary |
|---|---|
| public void replaceSubArray(Object array, int atIndex) Allows an array type to overwrite a segment of the array.
Will expand the array if
| |
| replaceSubArray | back to summary |
|---|---|
| public void replaceSubArray(int thisStart, int thisStop, Object srcArray, int srcStart, int srcStop) Replace a range of this array with another subarray.
| |
| setArray | back to summary |
|---|---|
| protected abstract void setArray(Object array) Set the backing array. This method is used by the type-agnostic base class code to set the array used for type-specific storage by the child class.
| |
| setNewBase | back to summary |
|---|---|
| private void setNewBase(int newCapacity) Replaces the existing base array in the subclass with a new base array resized to the specified capacity. | |
| setSize | back to summary |
|---|---|
| public void setSize(int count) Sets the number of values currently present in the array. If the new size is greater than the current size, the added values are initialized to the default values. If the new size is less than the current size, all values dropped from the array are discarded.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Provides a default comma-delimited representation of array.
| |
| trimToSize | back to summary |
|---|---|
| protected void trimToSize() Removes any excess capacity in the backing array so it is just big enough to hold the amount of data actually in the array. | |