|
|||||||||
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 | |
---|---|
java.lang.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,
java.lang.Object element)
Insert an object into the array |
void |
add(java.lang.Object o)
Add an element to the array. |
void |
addAll(java.util.Collection c)
Add all the elements of the specified Collection. |
void |
addAll(DynamicArray that)
Add all the elements from another array. |
void |
addAll(java.lang.Object[] that)
Add all the elements from another array. |
void |
addElement(java.lang.Object o)
Deprecated. use add instead |
void |
addElementIfAbsent(java.lang.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(java.lang.Object[] that)
Add all the elements from another array. |
void |
addElementsIfAbsent(java.lang.Object[] that)
Add all elements in the passed array if not already present. |
void |
addIdentical(java.lang.Object o)
Add an element to the array, if it isn't in the array |
abstract java.lang.Object[] |
alloc(int size)
Allocate the storage array. |
java.lang.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(java.lang.Object x)
Check if the array contains an object. |
boolean |
containsIdentical(java.lang.Object x)
Check if the array contains an object. |
java.lang.Object[] |
extend(java.lang.Object[] a,
java.lang.Object x)
A version of addElement for cases where all you've got is a Foo[] |
int |
indexOf(java.lang.Object o)
Find the location of an object in the array. |
void |
insertElementAt(java.lang.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. |
java.lang.Object |
remove(int index)
Remove an element from the array |
java.lang.Object |
removeElementAt(int index)
Remove an element from the array |
void |
setArray(java.lang.Object[] array)
Replace the storage array. |
void |
setCapacity(int size)
Change the size of the storage. |
java.util.List |
toList()
Converts the array to a List. |
java.lang.String |
toString()
Converts to a String. |
java.lang.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 java.lang.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(java.lang.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(java.lang.Object o)
o
- The element to addadd(Object)
public void add(java.lang.Object o)
o
- The element to addpublic void addElementIfAbsent(java.lang.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 addElementsIfAbsent(java.lang.Object[] that)
that
- (Object)
public void addIdentical(java.lang.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(java.util.Collection c)
c
- The Collection that we will copy frompublic void addElements(java.lang.Object[] that)
that
- The array that we will copy frompublic void addAll(java.lang.Object[] that)
that
- The array that we will copy frompublic boolean containsIdentical(java.lang.Object x)
x
- the object to check for
public boolean contains(java.lang.Object x)
x
- the object to check for
public int indexOf(java.lang.Object o)
o
- The object to look for
public java.lang.Object[] arrayCopy()
public java.lang.Object[] trim()
public java.lang.Object removeElementAt(int index)
index
- The index of the element to remove
java.lang.ArrayIndexOutOfBoundsException
- when index is larger than the
logical size of the array (inUse) or when it is less than 0.public java.lang.Object remove(int index)
index
- The index of the element to remove
java.lang.ArrayIndexOutOfBoundsException
- when index is larger than the
logical size of the array (inUse) or when it is less than 0.public void insertElementAt(java.lang.Object element, int index)
element
- The object to be insertedindex
- The location where the object should be inserted
java.lang.ArrayIndexOutOfBoundsException
- when index is larger than the
logical size of the array (inUse)public void add(int index, java.lang.Object element)
element
- The object to be insertedindex
- The location where the object should be inserted
java.lang.ArrayIndexOutOfBoundsException
- when index is larger than the
logical size of the array (inUse)public abstract java.lang.Object[] alloc(int size)
size
- The size of the array to allocate
public java.lang.Object[] extend(java.lang.Object[] a, java.lang.Object x)
a
- original arrayx
- element to add
a.length
+1 containing contents
of a
plus x as the last elementpublic java.lang.String toString()
toString
in class java.lang.Object
public java.util.List toList()
public void clear()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |