|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectariba.util.core.DynamicArray
public abstract class DynamicArray
A DynamicArray is like List except that is exposes the underlying array. DynamicArray is abstract so that clients can supply a method to allocate the array to be of a particular type. This gets back some of the runtime type checking that List loses, and allows more concise enumeration. Here is a sample concrete subclass:
public class IntegerArray extends DynamicArray
{
public Object[] alloc (int size)
{
return new Integer[ size ];
}
public final Integer[] array ()
{
return (Integer[]) array;
}
}
A use of IntegerArray might look like this:
IntegerArray v = Util.integerList();
v.addElement (Constants.getInteger(1));
v.addElement (Constants.getInteger(2));
Integer[] a = v.array();
for (int k = 0; k < v.inUse; k++) {
doIntegerOp(a[k]);
}
Using List, each element access in the loop would entail a procedure call and a cast.
Note that this is not a thread-safe implementation. Consumer is responsible for synchronizing the access to the object
| Field Summary | |
|---|---|
Object[] |
array
The storage array. |
int |
inUse
Deprecated. see inUse() |
| Constructor Summary | |
|---|---|
DynamicArray()
Create a DynamicArray with a default storage size. |
|
DynamicArray(int size)
Create a new DynamicArray |
|
| Method Summary | |
|---|---|
void |
add(int index,
Object element)
Insert an object into the array |
void |
add(Object o)
Add an element to the array. |
void |
addAll(Collection c)
Add all the elements of the specified Collection. |
void |
addAll(DynamicArray that)
Add all the elements from another array. |
void |
addAll(Object[] that)
Add all the elements from another array. |
void |
addElement(Object o)
Deprecated. use add instead |
void |
addElementIfAbsent(Object o)
Add an element to the array, if it isn't in the array |
void |
addElements(DynamicArray that)
Add all the elements from another array. |
void |
addElements(Object[] that)
Add all the elements from another array. |
void |
addIdentical(Object o)
Add an element to the array, if it isn't in the array |
abstract Object[] |
alloc(int size)
Allocate the storage array. |
Object[] |
arrayCopy()
Return a copy of the underlying array sized to fit the number of allocated elements. |
void |
clear()
Resets the array The size of underlying array remains the same |
boolean |
contains(Object x)
Check if the array contains an object. |
boolean |
containsIdentical(Object x)
Check if the array contains an object. |
Object[] |
extend(Object[] a,
Object x)
A version of addElement for cases where all you've got is a Foo[] |
int |
indexOf(Object o)
Find the location of an object in the array. |
void |
insertElementAt(Object element,
int index)
Insert an object into the array |
int |
inUse()
The number of members that are currently used. |
void |
makeRoomFor(int want)
Make sure there is room for additional elements. |
Object |
remove(int index)
Remove an element from the array |
Object |
removeElementAt(int index)
Remove an element from the array |
void |
setArray(Object[] array)
Replace the storage array. |
void |
setCapacity(int size)
Change the size of the storage. |
List |
toList()
Converts the array to a List. |
String |
toString()
Converts to a String. |
Object[] |
trim()
Make the allocated length equal the number of elements in use. |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public Object[] array
public int inUse
| Constructor Detail |
|---|
public DynamicArray()
public DynamicArray(int size)
size - The starting size of the storage array.
A size of -1 creates an instance with no array allocated.| Method Detail |
|---|
public int inUse()
public void setArray(Object[] array)
array - The new array to use as storagepublic void setCapacity(int size)
size - The new requested sizepublic void makeRoomFor(int want)
want - The number of elements we want to have space forpublic void addElement(Object o)
o - The element to addadd(Object)public void add(Object o)
o - The element to addpublic void addElementIfAbsent(Object o)
o - The element to add. If o is already in the array
then it is not added. o.equals() is used to compare elements.public void addIdentical(Object o)
o - The element to add. If o is already in the array
then it is not added. The == operator is used to
compare elements.public void addElements(DynamicArray that)
that - The array that we will copy frompublic void addAll(DynamicArray that)
that - The array that we will copy frompublic void addAll(Collection c)
c - The Collection that we will copy frompublic void addElements(Object[] that)
that - The array that we will copy frompublic void addAll(Object[] that)
that - The array that we will copy frompublic boolean containsIdentical(Object x)
x - the object to check for
public boolean contains(Object x)
x - the object to check for
public int indexOf(Object o)
o - The object to look for
public Object[] arrayCopy()
public Object[] trim()
public Object removeElementAt(int index)
index - The index of the element to remove
ArrayIndexOutOfBoundsException - when index is larger than the
logical size of the array (inUse) or when it is less than 0.public Object remove(int index)
index - The index of the element to remove
ArrayIndexOutOfBoundsException - when index is larger than the
logical size of the array (inUse) or when it is less than 0.
public void insertElementAt(Object element,
int index)
element - The object to be insertedindex - The location where the object should be inserted
ArrayIndexOutOfBoundsException - when index is larger than the
logical size of the array (inUse)
public void add(int index,
Object element)
element - The object to be insertedindex - The location where the object should be inserted
ArrayIndexOutOfBoundsException - when index is larger than the
logical size of the array (inUse)public abstract Object[] alloc(int size)
size - The size of the array to allocate
public Object[] extend(Object[] a,
Object x)
a - original arrayx - element to add
a.length+1 containing contents
of a plus x as the last elementpublic String toString()
toString in class Objectpublic List toList()
public void clear()
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||