Cloneablepublic class PlainIntArray extends Object implements Cloneable
int as the key and
value.
Values in this class are directly stored in arrays, without being wrapped into other types or automatically boxed.
This simplifies the data storage structure and saves memory.
All keys in this class are sorted in ascending order when they are stored, and the binary search algorithm is used to
efficiently search for a specified key.
In such data storage structures, keys and values can be iterated over based on indexes.
This class implements the Cloneable interface.
Pay attention to the following when using the PlainIntArray class:
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
INVALID_INDEX |
Indicates an invalid index used by index-related operations.
|
| 构造器 | 说明 |
|---|---|
PlainIntArray() |
Default constructor used to create a
PlainIntArray instance. |
PlainIntArray(int capacity) |
A constructor used to create a
PlainIntArray instance with a custom initial capacity. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
append(int key,
int value) |
Appends a key-value pair to
PlainIntArray. |
void |
clear() |
Clears all key-value pairs stored in the current
PlainIntArray object. |
PlainIntArray |
clone() |
Creates a clone of the current
PlainIntArray object. |
boolean |
contains(int key) |
Checks whether the current
PlainIntArray object contains the specified key. |
Optional<Integer> |
get(int key) |
Queries the value associated with a specified
key. |
int |
get(int key,
int defaultValue) |
Queries the value associated with the specified
key with the defaultValue parameter passed. |
int |
indexOfKey(int key) |
Queries the index for a specified
key. |
int |
indexOfValue(int value) |
Queries the index for a specified
value. |
boolean |
isEmpty() |
Checks whether the current
PlainIntArray object is empty. |
int |
keyAt(int index) |
Queries the key at a specified index.
|
int |
locate(int key) |
Searches for the index of a specified key.
|
void |
put(int key,
int value) |
Adds a key-value pair to
PlainIntArray. |
Optional<Integer> |
remove(int key) |
Removes the key-value pair based on a specified key.
|
Optional<Integer> |
removeAt(int index) |
Removes the key-value pair at a specified index.
|
void |
setValueAt(int index,
int value) |
Updates the value at a specified index.
|
int |
size() |
Obtains the total number of values stored in the current
PlainIntArray. |
String |
toString() |
Obtains a string representation of the
PlainIntArray object. |
int |
valueAt(int index) |
Queries the value at a specified index.
|
public static final int INVALID_INDEX
public PlainIntArray()
PlainIntArray instance.public PlainIntArray(int capacity)
PlainIntArray instance with a custom initial capacity.
This constructor allows you to customize the initial capacity of the PlainIntArray object.
capacity - Indicates the initial capacity to customize for the object.IllegalArgumentException - Throws this exception if the specified capacity is less than or equal to
0.public int locate(int key)
The binary search algorithm is used for searching. If no result is found, a negative value is returned. However, you can invert this negative value to obtain the index where the key should be stored in the array.
key - Indicates the key to search for.public void append(int key,
int value)
PlainIntArray.
If the key to be added is greater than any existing key in the array, this method is more efficient than
the put(int,int) method.
If the specified key is not the largest one in the array, the put(int,int) method is
automatically called to add the key-value pair.
key - Indicates the key to be added.value - Indicates the value associated with the key.public void clear()
PlainIntArray object.public PlainIntArray clone()
PlainIntArray object.public Optional<Integer> remove(int key)
public Optional<Integer> removeAt(int index)
index - Indicates the index of the key-value pair to remove.Optional object containing the removed value.IndexOutOfBoundsException - Throws this exception if the specified index is beyond the valid range.public int get(int key,
int defaultValue)
key with the defaultValue parameter passed.key - Indicates the key of the value to query.defaultValue - Indicates the default value.key; returns defaultValue if the key does not exist.public int indexOfKey(int key)
key.key - Indicates the key to query.INVALID_INDEX if the queried key does not exist.public int indexOfValue(int value)
value.value - Indicates the value to query.INVALID_INDEX if the queried value does not exist.public int keyAt(int index)
index - Indicates the index to query.IndexOutOfBoundsException - Throws this exception if the specified index is beyond the valid range.public int valueAt(int index)
index - Indicates the index to query.IndexOutOfBoundsException - Throws this exception if the specified index is beyond the valid range.public void put(int key,
int value)
PlainIntArray.
If the specified key already exists, its value will be replaced by value.
key - Indicates the key to be added.value - Indicates the value associated with the key.public void setValueAt(int index,
int value)
index - Indicates the index of the value to update.value - Indicates the new value.IndexOutOfBoundsException - Throws this exception if the specified index is beyond the valid range.public int size()
PlainIntArray.public boolean isEmpty()
PlainIntArray object is empty.true if no value is contained; returns false otherwise.public boolean contains(int key)
PlainIntArray object contains the specified key.key - Indicates the key to check.true if the specified key exists; returns false otherwise.