|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectch.javasoft.util.IntArray
public class IntArray
The IntArray is an dynamic size array for ints. Values can be
added causing the array to grow dynamically.
| Constructor Summary | |
|---|---|
IntArray()
Constructor for IntArray with default capacity |
|
IntArray(int capacity)
Constructor for IntArray with default capacity |
|
IntArray(int[] initialValues)
Constructor for IntArray with specified values. |
|
IntArray(int[] original,
int from,
int to)
Constructor for IntArray with specified values. |
|
| Method Summary | |
|---|---|
void |
add(int value)
Adds the specified value to the end of this int array |
boolean |
add(int index,
int value)
Adds the specified value at the given position of the array. |
void |
addAll(int... values)
Adds the specified values to the end of this int array |
void |
addAll(IntArray array)
Adds the values from array to the end of this int array |
void |
addAll(int from,
int to,
int... values)
Adds the specified values to the end of this int array, from index from (inclusive) to to (exclusive). |
void |
addAll(int from,
int to,
IntArray array)
Adds the values from array to the end of this int array |
int |
addToSorted(int value)
Adds the given value at the position where the value fits according to binarySearch(int). |
int |
binarySearch(int key)
Searches this int array for the specified value using the binary search algorithm. |
int |
binarySearch(int key,
int fromIndex,
int toIndex)
Searches this int array for the specified value using the binary search algorithm. |
void |
clear()
Removes all elements, but does not shrink the underlying array (that is, the capacity is kept as it is). |
IntArray |
clone()
Returns a clone of this int array |
static int[][] |
clone(int[][] arr)
Returns a deep clone of the given array |
protected void |
ensureCapacity(int size)
Internal method, called to ensure capacity size |
boolean |
equals(Object obj)
|
int |
first()
Returns the first element, or throws an IndexOutOfBoundsException
if the array is empty |
int |
get(int index)
Returns the value at the specified position |
int |
hashCode()
|
int |
indexOf(int value)
Returns the first index containing a value equal to the specified value. |
int |
indexOf(int fromIndex,
int value)
Returns the first index containing a value equal to the specified value starting at position fromIndex. |
protected int |
initialValue()
This method can be overriden to define an alternate initial value. |
boolean |
isEmpty()
Returns true if the array length is zero |
Iterator<Integer> |
iterator()
Returns an immutable iterator with the integer values of this array |
int |
last()
Returns the last element, or throws an IndexOutOfBoundsException
if the array is empty |
int |
length()
Returns the current length of this array, that is, the number of elements in the array. |
int |
remove(int index)
Removes the specified value and moves all values on the right one position leftwards. |
int |
removeLast()
Removes the last value, or throws an IndexOutOfBoundsException if
the array is empty. |
int |
set(int index,
int value)
Sets the value at the specified position. |
void |
sort(boolean ascending)
Sort the array ascending or descending. |
void |
sort(boolean ascending,
int start,
int end)
Sort the specified array range ascending or descending. |
IntArray |
subRange(int from)
Returns a new array instance containing a copy of a range of this array. |
IntArray |
subRange(int from,
int to)
Returns a new array instance containing a copy of a range of this array. |
static void |
swap(int[] arr,
int indexA,
int indexB)
Interchange the values at the specified array positions |
void |
swap(int indexA,
int indexB)
Interchange the values at the specified positions |
int[] |
toArray()
Clones the underlying array (trimmed to length) and returns it |
static int[][] |
toMatrix(Collection<int[]> mx,
boolean cloneArrays)
Converts the given collection of int arrays to a two dimensional int array. |
static int[][] |
toMatrix(Collection<IntArray> mx)
Converts the given collection of IntArray instances to a
two dimensional int array |
static int[][] |
toMatrix(IntArray[] mx)
Converts the given array of IntArray instances to a two
dimensional int array |
String |
toString()
Returns a string representation of the contents of this array. |
void |
trimToLength()
Fixes the capacity of the underlying array to the current length of this array. |
int[] |
yieldArray()
Returns the internal array after trimming it to the current length and returns it. |
| Methods inherited from class java.lang.Object |
|---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
|---|
public IntArray()
IntArray with default capacity
public IntArray(int capacity)
IntArray with default capacity
public IntArray(int[] initialValues)
IntArray with specified values. The given
array is not cloned, thus, changes to the array are also reflected in
the state this IntArray.
public IntArray(int[] original,
int from,
int to)
IntArray with specified values. The given
array is cloned.
Copies the specified range of the specified array into this new array. The initial index of the range (from) must lie between zero and original.length, inclusive. The value at original[from] is placed into the initial element of the copy (unless from == original.length or from == to). Values from subsequent elements in the original array are placed into subsequent elements in the copy. The final index of the range (to), which must be greater than or equal to from, may be greater than original.length, in which case 0d is placed in all elements of the copy whose index is greater than or equal to original.length - from. The length of the this array will be to - from.
original - the array from which a range is to be copiedfrom - the initial index of the range to be copied, inclusiveto - the final index of the range to be copied, exclusive.
(This index may lie outside the array.)| Method Detail |
|---|
public int length()
public boolean isEmpty()
length is zero
public int get(int index)
throws IndexOutOfBoundsException
IndexOutOfBoundsExceptionpublic int indexOf(int value)
value. If no such value is found, -1 is
returned.
value - the value to search for
value, or
-1 if no such value is found
public int indexOf(int fromIndex,
int value)
value starting at position fromIndex. If no
such value is found, -1 is returned.
fromIndex - the index (inclusive) at which the search is startedvalue - the value to search for
value, or
-1 if no such value is found
public int set(int index,
int value)
throws IndexOutOfBoundsException
index is
smaller than the current length(), the value at the specified
position is replaced by value. If index equal
to the length of this array, the value is appended. If it
is larger than length, it is also appended after filling the
gap positions with initial values.
index - the index at which value will be storedvalue - the new value to be set
IndexOutOfBoundsExceptionpublic void add(int value)
public void addAll(IntArray array)
array to the end of this int array
public void addAll(int from,
int to,
IntArray array)
array to the end of this int array
public void addAll(int... values)
public void addAll(int from,
int to,
int... values)
from (inclusive) to to (exclusive).
public boolean add(int index,
int value)
index is negative or larger than the current
length of this array, an
IndexOutOfBoundsException is thrown.
public int removeLast()
IndexOutOfBoundsException if
the array is empty.
public int remove(int index)
IndexOutOfBoundsException - if index is negative or
larger or equal to
lengthpublic void clear()
public int first()
IndexOutOfBoundsException
if the array is empty
public int last()
IndexOutOfBoundsException
if the array is empty
public void swap(int indexA,
int indexB)
protected void ensureCapacity(int size)
size
size - the desired capacityprotected int initialValue()
set(int, int) is used with an index
larger than the length of the array.
Default initial value is 0
public int[] toArray()
public int[] yieldArray()
IntArray will be empty after this
operation.
public IntArray clone()
clone in class Objectpublic IntArray subRange(int from)
from (inclusive) to the end of this
array.
from - the start index of the range, inclusive
public IntArray subRange(int from,
int to)
from (inclusive) to to
(exclusive).
from - the start index of the range, inclusiveto - the end index of the range, exclusive
public Iterator<Integer> iterator()
iterator in interface Iterable<Integer>public static int[][] toMatrix(IntArray[] mx)
IntArray instances to a two
dimensional int array
mx - the array of IntArray instances to convert to
a 2-dim array
public static int[][] toMatrix(Collection<IntArray> mx)
IntArray instances to a
two dimensional int array
mx - the collection of arrays to convert to a 2-dim array
public static int[][] toMatrix(Collection<int[]> mx,
boolean cloneArrays)
mx - the collection of arrays to convert to a 2-dim arraycloneArrays - if true, the source arrays are cloned
public static void swap(int[] arr,
int indexA,
int indexB)
public static int[][] clone(int[][] arr)
public void trimToLength()
length of this array. Note that this operation might
be expensive due to array copying.
public int binarySearch(int key)
throws IllegalStateException
sort(boolean) method) prior to making this
call. If it is not sorted, the results are undefined. If the array
contains multiple elements with the specified value, there is no
guarantee which one will be found.
key - the value to be searched for.
IllegalStateExceptionArrays.binarySearch(int[], int, int, int)
public int binarySearch(int key,
int fromIndex,
int toIndex)
throws IllegalStateException
sort(boolean) method) prior to making this
call. If it is not sorted, the results are undefined. If the array
contains multiple elements with the specified value, there is no
guarantee which one will be found.
key - the value to be searched for.fromIndex - the index of the first element (inclusive) to be
searchedtoIndex - the index of the last element (exclusive) to be searched
IllegalStateExceptionArrays.binarySearch(int[], int, int, int)public int addToSorted(int value)
binarySearch(int). If this int array is not sorted
ascending, the result is undefined.
value - the value to insert
public void sort(boolean ascending)
binarySearch(int) and
addToSorted(int) methods can be used
ascending - true for ascending sort order
public void sort(boolean ascending,
int start,
int end)
binarySearch(int) and
addToSorted(int) methods can be used.
This method should only be used if one knows that only the defined
interval needs sorting. Otherwise, sort(boolean) should be used
instead.
ascending - true for ascending sort orderstart - the index where the sorting starts (inclusive)end - the index where the sorting ends (exclusive)public int hashCode()
hashCode in class Objectpublic boolean equals(Object obj)
equals in class Objectpublic String toString()
toString in class ObjectArrays.toString(int[])
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||