Class AbstractArray
- All Implemented Interfaces:
Serializable
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.
- Author:
- Clark Updike
- See Also:
-
Constructor Summary
ConstructorDescriptionAbstractArray
(int size) Use when the subclass has a preexisting array.AbstractArray
(Class type) Creates the managed array with a default size of 10.AbstractArray
(Class type, int size) Creates the managed array with the specified size.AbstractArray
(Class type, int[] dimensions) Construtor for multi-dimensional array types.AbstractArray
(AbstractArray toCopy) Since AbstractArray can support a clone method, this facilitates subclasses that want to implement clone (poor man's cloning). -
Method Summary
Modifier and TypeMethodDescriptionvoid
appendArray
(Object ofArrayType) Appends the supplied array, which must be an array of the same type asthis
, to the end ofthis
.void
clear()
Set the array to the empty state, clearing all the data out and nulling objects (or "zero-ing" primitives).Constructs and returns a simple array containing the same data as held in this growable array.int
Returns the modification count increment, which is used byAbstractList
subclasses to adjustmodCount
AbstractList
uses it'smodCount
field to invalidate concurrent operations (like iteration) that should fail if the underlying array changes structurally during the operation.int
getSize()
Get the number of values currently present in the array.void
remove
(int index) Remove a value from the array.void
remove
(int start, int stop) Removes a range from the array at the specified indices.void
replaceSubArray
(int thisStart, int thisStop, Object srcArray, int srcStart, int srcStop) Replace a range of this array with another subarray.void
replaceSubArray
(Object array, int atIndex) Allows an array type to overwrite a segment of the array.void
setSize
(int count) Sets the number of values currently present in the array.toString()
Provides a default comma-delimited representation of array.
-
Constructor Details
-
AbstractArray
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); }
- Parameters:
toCopy
-
-
AbstractArray
public AbstractArray(int size) Use when the subclass has a preexisting array.- Parameters:
size
- the initial size of the array
-
AbstractArray
Creates the managed array with a default size of 10.- Parameters:
type
- array element type (primitive type or object class)
-
AbstractArray
Construtor for multi-dimensional array types. For example,char[][]
. This class only manages the top level dimension of the array. For single dimension arrays (the more typical usage), use the other constructors.- Parameters:
type
- Array element type (primitive type or object class).dimensions
- An int array specifying the dimensions. For a 2D array, something likenew int[] {10,0}
to create 10 elements each of which can hold an reference to an array of the same type.- See Also:
-
AbstractArray
Creates the managed array with the specified size.- Parameters:
type
- array element type (primitive type or object class)size
- number of elements initially allowed in array
-
-
Method Details
-
appendArray
Appends the supplied array, which must be an array of the same type asthis
, to the end ofthis
.AbstractList
subclasses should update theirmodCount
after calling this method.- Parameters:
ofArrayType
- the array to append
-
clear
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
modCountIncr
to1
even thoughjava.util.ArrayList
would.AbstractList
subclasses should update theirmodCount
after calling this method. -
copyArray
Constructs and returns a simple array containing the same data as held in this growable array.- Returns:
- array containing a shallow copy of the data.
-
remove
public void remove(int index) Remove a value from the array. All values above the index removed are moved down one index position.AbstractList
subclasses should always increment theirmodCount
method after calling this, asremove
always causes a structural modification.- Parameters:
index
- index number of value to be removed
-
remove
public void remove(int start, int stop) Removes a range from the array at the specified indices.- Parameters:
start
- inclusivestop
- exclusive
-
replaceSubArray
Allows an array type to overwrite a segment of the array. Will expand the array if(atIndex + 1) + ofArrayType
's length is greater than the current length.AbstractList
subclasses should update theirmodCount
after calling this method.- Parameters:
array
-atIndex
-
-
replaceSubArray
public void replaceSubArray(int thisStart, int thisStop, Object srcArray, int srcStart, int srcStop) Replace a range of this array with another subarray.- Parameters:
thisStart
- the start index (inclusive) of the subarray in this array to be replacedthisStop
- the stop index (exclusive) of the subarray in this array to be replacedsrcArray
- the source array from which to copysrcStart
- the start index (inclusive) of the replacement subarraysrcStop
- the stop index (exclusive) of the replacement subarray
-
setSize
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.AbstractList
subclasses should update theirmodCount
after calling this method.- Parameters:
count
- number of values to be set
-
getSize
public int getSize()Get the number of values currently present in the array.- Returns:
- count of values present
-
toString
Provides a default comma-delimited representation of array. -
getModCountIncr
public int getModCountIncr()Returns the modification count increment, which is used byAbstractList
subclasses to adjustmodCount
AbstractList
uses it'smodCount
field to invalidate concurrent operations (like iteration) that should fail if the underlying array changes structurally during the operation.- Returns:
- the modification count increment (0 if no change, 1 if changed)
-