public class IntArray extends Object
int arrays. This class encapsulates commonly used operations on IntArray for
you to invoke. It also supports basic operations regarding generic stacks, so that you can use them as int
stacks.
When the int primitive data type is stored into a generic collection class, it is automatically converted
into a corresponding data type. This process is known as autoboxing, which however, will deteriorate performance to
some extent. To resolve this issue, this class is used to store the int primitive data as a collection class,
without the autoboxing process.
This class supports automatic scaling of arrays so that you do not need to worry about the array capacity.
Pay attention to the following when using the IntArray class:
This class is non-thread safe and is not recommended for service concurrency.
| 限定符和类型 | 字段 | 说明 |
|---|---|---|
static int |
ERROR_INDEX |
Indicates an invalid index used by index-related operations.
|
| 构造器 | 说明 |
|---|---|
IntArray() |
Default constructor used to create an
IntArray instance with an initial capacity of 32. |
IntArray(int capacity) |
A constructor used to create an
IntArray instance with a custom initial capacity. |
IntArray(int... otherArray) |
Creates an
IntArray object based on a size-mutable int array. |
IntArray(int[] otherArray,
int begin,
int count) |
Creates an
IntArray object based on an existing int array with a specified range. |
IntArray(int capacity,
int value) |
A constructor used to create an
IntArray instance with a custom capacity and an initial value. |
IntArray(List<Integer> intList) |
Creates an
IntArray object based on an existing Integer list. |
IntArray(IntArray another) |
Creates an
IntArray object by copying parameters from an existing IntArray object. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
append(IntArray other) |
Appends the values of a specified
IntArray object to the current IntArray object in sequence. |
int |
back() |
Obtains the last value in the array of the current
IntArray object. |
int |
binarySearchInInsArray(int value) |
Searches for a specified value using the binary search algorithm.
|
void |
clear() |
Clears all values stored in the current
IntArray object. |
Object |
clone() |
Creates a clone of the current
IntArray object. |
void |
deleteFirstValue(int value) |
Deletes the first value matched with the specified value.
|
void |
deleteIndex(int index) |
Deletes the value at a specified index.
|
void |
deleteRangeIn(int low,
int high) |
Deletes values in a specified range with both the start and end indexes included.
|
boolean |
empty() |
Checks whether the current
IntArray object is empty. |
boolean |
equals(int[] otherArrays) |
Compares values in the current
IntArray object with an int array and returns the comparison
result. |
boolean |
equals(Object obj) |
Compares the current
IntArray object with the specified object and returns the comparison
result. |
int |
getAt(int index) |
Obtains the value at a specified index.
|
int |
getCapacity() |
Obtains the capacity of the current
IntArray object. |
int |
hashCode() |
Obtains the hash code of the current
IntArray object. |
int |
indexOf(int value) |
Obtains the index of the first matched value in the current
IntArray object. |
void |
insert(int index,
int value) |
Inserts a value to a specified index.
|
int |
lastIndexOf(int value) |
Obtains the index of the last matched value in the current
IntArray object. |
int |
popBack() |
Obtains and deletes the last value in the array of the current
IntArray object. |
void |
pushBack(int value) |
Adds a specified value to the end of the array in the current
IntArray object. |
void |
setAt(int index,
int value) |
Updates the value at a specified index.
|
int |
size() |
Obtains the total number of values stored in the current
IntArray. |
void |
sortIns() |
Sorts the arrays in the current
IntArray object in ascending order. |
void |
swap(IntArray other) |
Exchanges elements between a specified
IntArray object and the current IntArray object, including
the array, size, and capacity. |
Integer[] |
toIntegerArray() |
Copies the values in the current
IntArray object and converts them into an Integer array. |
String |
toString() |
Obtains a string representation of the current
IntArray object. |
String |
toString(String sep) |
Obtains a string representation of the current
IntArray object with a specified separator. |
public static final int ERROR_INDEX
public IntArray()
IntArray instance with an initial capacity of 32.public IntArray(int capacity)
IntArray instance with a custom initial capacity.capacity - Indicates the initial capacity to customize for the object.public IntArray(int capacity,
int value)
IntArray instance with a custom capacity and an initial value.capacity - Indicates the initial size to customize for the array.value - Indicates the initial value of the array.public IntArray(IntArray another)
IntArray object by copying parameters from an existing IntArray object.another - Indicates the existing IntArray object.NullPointerException - Throws this exception if another is null.public IntArray(int... otherArray)
IntArray object based on a size-mutable int array.otherArray - Indicates the size-mutable int array.public IntArray(int[] otherArray,
int begin,
int count)
IntArray object based on an existing int array with a specified range.otherArray - Indicates the existing int array.begin - Indicates the start index of the existing array.count - Indicates the number of values stored in the existing array.IllegalArgumentException - Throws this exception if the specified otherArray is null.ArrayIndexOutOfBoundsException - Throws this exception if the specified begin or count is out of bound.public int hashCode()
IntArray object.hashCode 在类中 ObjectIntArray object.Object.equals(java.lang.Object),
System.identityHashCode(java.lang.Object)public boolean equals(int[] otherArrays)
IntArray object with an int array and returns the comparison
result.otherArrays - Indicates the int array to compare.true if the two have the same values in sequence.public boolean equals(Object obj)
IntArray object with the specified object and returns the comparison
result.equals 在类中 Objectobj - Indicates the specified object to compare.true if the two have the same values in sequence.Object.hashCode(),
HashMappublic Object clone()
IntArray object.public String toString()
IntArray object.
When being converted into a string, the values are enclosed in square brackets ([]) and separated by commas (,),
for example, [1, 2, 3, 4, 5].
public String toString(String sep)
IntArray object with a specified separator.
When being converted into a string, the values are enclosed in square brackets ([]) and separated by the separator. For example, if the specified separator is a semicolon (;), the string representation is in the format of [1;2;3;4;5].
sep - Indicates the separator for the values in the string representation.IntArray object, with values separated using the
specified separator.public int getCapacity()
IntArray object.
The return value of this method is the capacity of the object, but not the actual size occupied by the values in this object.
IntArray object.public void insert(int index,
int value)
If the specified index has a value, this index and its followed values are moved backward by one index.
index - The index.value - A value.public int getAt(int index)
index - Indicates the index of the value to query.ArrayIndexOutOfBoundsException - Throws this exception if the specified index is beyond the valid range.public void setAt(int index,
int value)
Different from the insert(int,int) method, this method replaces the existing value with the new one if
the specified index exists.
index - Indicates the index of the value to update.value - Indicates the value to be updated.ArrayIndexOutOfBoundsException - Throws this exception if the specified index is beyond the valid range.public int indexOf(int value)
IntArray object.value - Indicates the value to query.ERROR_INDEX if the value does not exist.public int lastIndexOf(int value)
IntArray object.value - Indicates the value to query.ERROR_INDEX if the value does not exist.public void clear()
IntArray object.public int size()
IntArray.IntArray.public boolean empty()
IntArray object is empty.true if the current IntArray object is empty.public void pushBack(int value)
IntArray object.value - Indicates the value to add.public int popBack()
IntArray object. This value is deleted
from the array when it is returned.IntArray object.public int back()
IntArray object. This value will not be deleted when
it is returned.IntArray object.public void swap(IntArray other)
IntArray object and the current IntArray object, including
the array, size, and capacity.other - Indicates the IntArray object to be exchanged.NullPointerException - Throws this exception if another is null.public void append(IntArray other)
IntArray object to the current IntArray object in sequence.other - Indicates the IntArray object.NullPointerException - Throws this exception if another is null.public Integer[] toIntegerArray()
IntArray object and converts them into an Integer array.Integer array that contains the values copied from the current IntArray
object.public void deleteFirstValue(int value)
value - Indicates the value to delete.public void deleteIndex(int index)
index - Indicates the index of the value to delete.ArrayIndexOutOfBoundsException - Throws this exception if the specified index is beyond the valid range.public void deleteRangeIn(int low,
int high)
low - Indicates the start index of the values to delete.high - Indicates the end index of the values to delete.ArrayIndexOutOfBoundsException - Throws this exception if the specified index is beyond the valid range.public void sortIns()
IntArray object in ascending order.public int binarySearchInInsArray(int value)
IntArray object.
Note that the binary search must be performed on ordered arrays. Therefore, before calling this method, you must
use the sortIns() method to sort values in the object. Otherwise, the returned result is uncertain.
value - Indicates the value to search for.