public class SparseIntHashArray extends Object implements SparseArray<Integer>, Serializable
int array. This class offers amortized constant time
access to array indices at the expense of space.
Instance offer a space savings of retaining only the non-zero indices and
values. For large array with only a few values set, this offers a huge
savings. However, as the cardinality of the array grows in relation to its
size, a dense int[] array will offer better performance in both space
and time. This is especially true if the sparse array instance approaches a
cardinality to size ratio of .5.
This class offers much better performance than SparseIntArray, but
will use significantly more space as the cardinality increases. In addition,
this class will marshall int primitives into Integer objects.
SparseArray,
SparseIntArray,
Serialized Form| Constructor and Description |
|---|
SparseIntHashArray()
Creates a sparse
int array that grows to the maximum size set by
Integer.MAX_VALUE. |
SparseIntHashArray(int length)
Creates a sparse
int array with a fixed length |
SparseIntHashArray(int[] array)
Creates a sparse array copy of the provided array, retaining only the
non-zero values.
|
| Modifier and Type | Method and Description |
|---|---|
int |
cardinality()
Returns the number of non-zero values in this sparse array
|
Integer |
get(int index)
Returns the value of this array at the index.
|
int[] |
getElementIndices()
Returns the indices of the array that contain non-
0 values. |
int |
length()
Returns the maximum length of this array.
|
void |
set(int index,
Integer value)
Sets the object as the value at the index.
|
<E> E[] |
toArray(E[] array)
Fills the provided array with the values contained in this array that fit
within the length of the provided array.
|
public SparseIntHashArray()
int array that grows to the maximum size set by
Integer.MAX_VALUE.public SparseIntHashArray(int length)
int array with a fixed lengthpublic SparseIntHashArray(int[] array)
public int cardinality()
cardinality in interface SparseArray<Integer>public Integer get(int index)
get in interface SparseArray<Integer>index - the position in the arraypublic int[] getElementIndices()
0 values.getElementIndices in interface SparseArray<Integer>public int length()
length in interface SparseArray<Integer>public void set(int index,
Integer value)
set in interface SparseArray<Integer>index - an index in the arrayvalue - the valuepublic <E> E[] toArray(E[] array)
toArray in interface SparseArray<Integer>Copyright © 2012. All Rights Reserved.