T
- the element type of the array.public final class Array<T> extends Object implements MSeq<T>, RandomAccess
Constructor and Description |
---|
Array(Collection<? extends T> values)
Deprecated.
Use
of(Collection) instead. |
Array(int length)
Create a new array with the given length.
|
Array(T first)
Deprecated.
Use
of(Object...) instead. |
Array(T[] values)
Deprecated.
Use
of(Object...) instead. |
Array(T first,
T second)
Deprecated.
Use
of(Object...) instead. |
Array(T first,
T second,
T third)
Deprecated.
Use
of(Object...) instead. |
Array(T first,
T second,
T third,
T fourth)
Deprecated.
Use
of(Object...) instead. |
Array(T first,
T second,
T third,
T fourth,
T fifth)
Deprecated.
Use
of(Object...) instead. |
Array(T first,
T second,
T third,
T fourth,
T fifth,
T... rest)
Deprecated.
Use
of(Object...) instead. |
Modifier and Type | Method and Description |
---|---|
Array<T> |
add(Array<? extends T> array)
Create a new array which contains the values of
this and the
given array . |
Array<T> |
add(Collection<? extends T> values)
Create a new array which contains the values of
this and the
given values . |
Array<T> |
add(T value)
Create a new array which contains the values of
this and the
given value . |
List<T> |
asList()
Returns a fixed-size list backed by the specified sequence.
|
static Array<Boolean> |
box(boolean... values)
Boxes the given native array into an
Array<Boolean> . |
static Array<Character> |
box(char... values)
Boxes the given native array into an
Array<Char> . |
static Array<Double> |
box(double... values)
Boxes the given native array into an
Array<Double> . |
static Array<Float> |
box(float... values)
Boxes the given native array into an
Array<Float> . |
static Array<Integer> |
box(int... values)
Boxes the given native array into an
Array<Integer> . |
static Array<Long> |
box(long... values)
Boxes the given native array into an
Array<Long> . |
static Array<Short> |
box(short... values)
Boxes the given native array into an
Array<Short> . |
boolean |
contains(Object element)
Returns
true if this sequence contains the specified element. |
Array<T> |
copy()
Return a new copy of type
T . |
static <T> Array<T> |
empty()
Return the empty array.
|
boolean |
equals(Object obj)
Compares the specified object with this sequence for equality.
|
Array<T> |
fill(Factory<? extends T> factory)
Fill the sequence with values generated by the given factory.
|
Array<T> |
filter(Function<? super T,Boolean> predicate)
Selects all elements of this list which satisfy a predicate.
|
boolean |
forall(Function<? super T,Boolean> predicate)
Deprecated.
Align the naming with the upcoming JDK 1.8 release. Use
forAll(Function) instead. |
boolean |
forAll(Function<? super T,Boolean> predicate)
Tests whether a predicate holds for all elements of this sequence.
|
<R> void |
foreach(Function<? super T,? extends R> function)
Deprecated.
Align the naming with the upcoming JDK 1.8 release. Use
forEach(Function) instead. |
<R> void |
forEach(Function<? super T,? extends R> function)
Applies a
function to all elements of this sequence. |
T |
get(int index)
Return the value at the given
index . |
int |
hashCode()
Returns the hash code value for this sequence.
|
int |
indexOf(Object element)
Returns the index of the first occurrence of the specified element
in this sequence, or -1 if this sequence does not contain the element.
|
int |
indexOf(Object element,
int start)
Returns the index of the first occurrence of the specified element
in this sequence, or -1 if this sequence does not contain the element.
|
int |
indexOf(Object element,
int start,
int end)
Returns the index of the first occurrence of the specified element
in this sequence, or -1 if this sequence does not contain the element.
|
int |
indexWhere(Function<? super T,Boolean> predicate)
Returns the index of the first element on which the given predicate
returns
true , or -1 if the predicate returns false for every
sequence element. |
int |
indexWhere(Function<? super T,Boolean> predicate,
int start)
Returns the index of the first element on which the given predicate
returns
true , or -1 if the predicate returns false for every
sequence element. |
int |
indexWhere(Function<? super T,Boolean> predicate,
int start,
int end)
Returns the index of the first element on which the given predicate
returns
true , or -1 if the predicate returns false for every
sequence element. |
Iterator<T> |
iterator() |
<B> Iterator<B> |
iterator(Function<? super T,? extends B> converter)
Return an iterator with the new type
B . |
int |
lastIndexOf(Object element)
Returns the index of the last occurrence of the specified element
in this sequence, or -1 if this sequence does not contain the element.
|
int |
lastIndexOf(Object element,
int end)
Returns the index of the last occurrence of the specified element
in this sequence, or -1 if this sequence does not contain the element.
|
int |
lastIndexOf(Object element,
int start,
int end)
Returns the index of the last occurrence of the specified element
in this sequence, or -1 if this sequence does not contain the element.
|
int |
lastIndexWhere(Function<? super T,Boolean> predicate)
Returns the index of the last element on which the given predicate
returns
true , or -1 if the predicate returns false for every
sequence element. |
int |
lastIndexWhere(Function<? super T,Boolean> predicate,
int end)
Returns the index of the last element on which the given predicate
returns
true , or -1 if the predicate returns false for every
sequence element. |
int |
lastIndexWhere(Function<? super T,Boolean> predicate,
int start,
int end)
Returns the index of the last element on which the given predicate
returns
true , or -1 if the predicate returns false for every
sequence element. |
int |
length()
Return the length of this sequence.
|
ListIterator<T> |
listIterator()
Returns a list iterator over the elements in this sequence (in proper
sequence).
|
<B> Array<B> |
map(Function<? super T,? extends B> mapper)
Builds a new sequence by applying a function to all elements of this
sequence.
|
static <T> Array<T> |
of(Collection<? extends T> values)
Create a new Array from the values of the given
Collection . |
static <T> Array<T> |
of(Seq<T> values)
Create a new Array from the values of the given
Seq . |
static <T> Array<T> |
of(T... values)
Create a new array from the given values.
|
Array<T> |
reverse()
Reverses the given array in place.
|
Array<T> |
reverse(int from,
int to)
Reverses the part of the array determined by the to indexes.
|
void |
set(int index,
T value)
Set the
value at the given index . |
Array<T> |
setAll(Iterable<? extends T> values)
Fills the sequence with values of the given iterable.
|
Array<T> |
setAll(Iterator<? extends T> it)
Fills the sequence with values of the given iterator.
|
Array<T> |
setAll(T value)
Set all sequence elements to the given
value . |
Array<T> |
setAll(T[] values)
Fill the sequence with the given values.
|
Array<T> |
shuffle()
Randomize this array using the registered
Random object. |
Array<T> |
shuffle(Random random)
Randomize this array using the given
Random object. |
void |
sort()
Sorts the array of objects into ascending order, according to the natural
ordering of its elements.
|
void |
sort(Comparator<? super T> comparator)
Sorts the array of objects according to the order induced by the specified
comparator.
|
void |
sort(int from,
int to)
Sorts the array of objects into ascending order, according to the natural
ordering of its elements.
|
void |
sort(int from,
int to,
Comparator<? super T> comparator)
Sorts the array of objects according to the order induced by the specified
comparator.
|
Array<T> |
subSeq(int start)
Returns a view of the portion of this sequence between the specified
start , inclusive, and end , exclusive. |
Array<T> |
subSeq(int start,
int end)
Returns a view of the portion of this sequence between the specified
start , inclusive, and end , exclusive. |
void |
swap(int i,
int j)
Swap the elements at the two positions.
|
void |
swap(int start,
int end,
Array<T> other,
int otherStart) |
void |
swap(int start,
int end,
MSeq<T> other,
int otherStart)
Swap a given range with a range of the same size with another array.
|
Object[] |
toArray()
Return an array containing all of the elements in this sequence in right
order.
|
T[] |
toArray(T[] array)
Return an array containing all of the elements in this sequence in right
order; the runtime type of the returned array is that of the specified
array.
|
ISeq<T> |
toISeq()
Return a read-only projection of this sequence.
|
String |
toString() |
String |
toString(String separator)
Create a string representation of the given sequence.
|
String |
toString(String prefix,
String separator,
String suffix)
Create a string representation of the given sequence.
|
static boolean[] |
unboxBoolean(Array<Boolean> values)
Unboxes the given array to the corresponding native version.
|
static char[] |
unboxChar(Array<Character> values)
Unboxes the given array to the corresponding native version.
|
static double[] |
unboxDouble(Array<Double> values)
Unboxes the given array to the corresponding native version.
|
static float[] |
unboxFloat(Array<Float> values)
Unboxes the given array to the corresponding native version.
|
static int[] |
unboxInt(Array<Integer> values)
Unboxes the given array to the corresponding native version.
|
static long[] |
unboxLong(Array<Long> values)
Unboxes the given array to the corresponding native version.
|
static short[] |
unboxShort(Array<Short> values)
Unboxes the given array to the corresponding native version.
|
static <T> Array<T> |
valueOf(Collection<? extends T> values)
Deprecated.
Use
of(java.util.Collection) instead. |
static <T> Array<T> |
valueOf(Seq<T> values)
Deprecated.
Use
of(Seq) instead. |
static <T> Array<T> |
valueOf(T... values)
Deprecated.
Use
of(Object[]) instead. |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
contains, equals, forall, forAll, foreach, forEach, get, hashCode, indexOf, indexOf, indexOf, indexWhere, indexWhere, indexWhere, iterator, lastIndexOf, lastIndexOf, lastIndexOf, lastIndexWhere, lastIndexWhere, lastIndexWhere, length, toArray, toArray, toString, toString
public Array(int length)
length
- the array length.NegativeArraySizeException
- if the specified length
is negative@Deprecated public Array(T first)
of(Object...)
instead.first
- the only element of the array.@Deprecated public Array(T first, T second)
of(Object...)
instead.first
- first array element.second
- second array element.@Deprecated public Array(T first, T second, T third)
of(Object...)
instead.first
- first array element.second
- second array element.third
- third array element.@Deprecated public Array(T first, T second, T third, T fourth)
of(Object...)
instead.first
- first array element.second
- second array element.third
- third array element.fourth
- fourth array element.@Deprecated public Array(T first, T second, T third, T fourth, T fifth)
of(Object...)
instead.first
- first array element.second
- second array element.third
- third array element.fourth
- fourth array element.fifth
- fifth array element.@Deprecated @SafeVarargs public Array(T first, T second, T third, T fourth, T fifth, T... rest)
of(Object...)
instead.first
- first array element.second
- second array element.third
- third array element.fourth
- fourth array element.fifth
- fifth array element.rest
- the rest of the array element.NullPointerException
- if the rest
array is null
.@Deprecated public Array(T[] values)
of(Object...)
instead.values
- the array values.NullPointerException
- if the values
array is null
.@Deprecated public Array(Collection<? extends T> values)
of(Collection)
instead.values
- the array values.NullPointerException
- if the values
array is null
.public static <T> Array<T> empty()
T
- the element type.public Array<T> filter(Function<? super T,Boolean> predicate)
predicate
- the predicate used to test elements.predicate
. The order of the elements is
preserved.NullPointerException
- if the given predicate
is
null
.public void set(int index, T value)
MSeq
value
at the given index
.public void sort()
Sorts the array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface. Furthermore, all elements in the array must be mutually comparable.
The sorting algorithm is the Quicksort.ClassCastException
- if the array contains elements that are not
mutually comparable (for example, strings and integers).public void sort(int from, int to)
Sorts the array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface. Furthermore, all elements in the array must be mutually comparable.
The sorting algorithm is the Quicksort.from
- the index of the first element (inclusive) to be sorted.to
- the index of the last element (exclusive) to be sorted.IndexOutOfBoundsException
- if from < 0 or to > length()
IllegalArgumentException
- if from > to
ClassCastException
- if the array contains elements that are not
mutually comparable (for example, strings and integers).public void sort(Comparator<? super T> comparator)
Sorts the array of objects according to the order induced by the specified comparator. All elements in the array must be mutually comparable by the specified comparator.
The sorting algorithm is the Quicksort.NullPointerException
- if the given comparator
is
null
.ClassCastException
- if the array contains elements that are not
mutually comparable (for example, strings and integers).public void sort(int from, int to, Comparator<? super T> comparator)
Sorts the array of objects according to the order induced by the specified comparator. All elements in the array must be mutually comparable by the specified comparator.
The sorting algorithm is the Timsort.from
- the index of the first element (inclusive) to be sorted.to
- the index of the last element (exclusive) to be sorted.NullPointerException
- if the given comparator
is
null
.IndexOutOfBoundsException
- if from < 0 or to > length()
IllegalArgumentException
- if from > to
ClassCastException
- if the array contains elements that are not
mutually comparable (for example, strings and integers).Arrays.sort(Object[], int, int, Comparator)
public Array<T> reverse(int from, int to)
from
- the first index (inclusive)to
- the second index (exclusive)IllegalArgumentException
- if from > toIndexOutOfBoundsException
- if from < 0 or
to > a.lengthpublic void swap(int i, int j)
MSeq
public void swap(int start, int end, MSeq<T> other, int otherStart)
MSeq
start end | | this: +---+---+---+---+---+---+---+---+---+---+---+---+ +---------------+ +---------------+ other: +---+---+---+---+---+---+---+---+---+---+---+---+ | otherStart
public void swap(int start, int end, Array<T> other, int otherStart)
MSeq.swap(int, int, MSeq, int)
public Array<T> shuffle(Random random)
Random
object. The used
shuffling algorithm is from D. Knuth TAOCP, Seminumerical Algorithms,
Third edition, page 142, Algorithm S (Selection sampling technique).random
- the Random
object to use for randomize.NullPointerException
- if the give random object is null
.public Array<T> shuffle()
Random
object.
The used shuffling algorithm is from D. Knuth TAOCP, Seminumerical
Algorithms, Third edition, page 142, Algorithm S (Selection sampling
technique).public Array<T> setAll(T value)
MSeq
value
.public Array<T> setAll(Iterator<? extends T> it)
MSeq
public Array<T> setAll(Iterable<? extends T> values)
MSeq
public Array<T> setAll(T[] values)
MSeq
public Array<T> fill(Factory<? extends T> factory)
MSeq
public ISeq<T> toISeq()
MSeq
ISeq
.public Array<T> add(T value)
this
and the
given value
. The length of the new array is
this.length() + 1
. The returned array is not sealed.value
- the value to append to this array.this
and the
given value
public Array<T> add(Array<? extends T> array)
this
and the
given array
. The length of the new array is
this.length() + array.length()
. The returned array is not sealed.array
- the array to append to this array.this
and the
given array
NullPointerException
- if the arrays
is null
.public Array<T> add(Collection<? extends T> values)
this
and the
given values
. The length of the new array is
this.length() + values.size()
. The returned array is not sealed.values
- the array to append to this array.this
and the
given array
NullPointerException
- if the values
is null
.public <B> Array<B> map(Function<? super T,? extends B> mapper)
Seq
map
in interface MSeq<T>
map
in interface Seq<T>
B
- the element type of the returned collection.mapper
- the function to apply to each element.public Array<T> copy()
Copyable
T
.public Array<T> subSeq(int start, int end)
Seq
start
, inclusive, and end
, exclusive. (If start
and end
are equal, the returned sequence has the length zero.) The
returned sequence is backed by this sequence, so non-structural changes in the
returned sequence are reflected in this array, and vice-versa.
This method eliminates the need for explicit range operations (of the
sort that commonly exist for arrays). Any operation that expects an array
can be used as a range operation by passing an sub sequence view instead of
an whole sequence.public Array<T> subSeq(int start)
Seq
start
, inclusive, and end
, exclusive. (If start
and end
are equal, the returned sequence has the length zero.) The
returned sequence is backed by this sequence, so non-structural changes
in the returned sequence are reflected in this sequence, and vice-versa.
This method eliminates the need for explicit range operations (of the
sort that commonly exist for arrays). Any operation that expects an sequence
can be used as a range operation by passing an sub sequence view instead of
an whole sequence.public List<T> asList()
Seq
RandomAccess
.public ListIterator<T> listIterator()
MSeq
listIterator
in interface MSeq<T>
@SafeVarargs public static <T> Array<T> of(T... values)
values
- the array values.NullPointerException
- if the values
array is null
.@Deprecated @SafeVarargs public static <T> Array<T> valueOf(T... values)
of(Object[])
instead.public static <T> Array<T> of(Collection<? extends T> values)
Collection
. The
order of the elements are determined by the iterator of the Collection.values
- the array values.NullPointerException
- if the values
array is null
.@Deprecated public static <T> Array<T> valueOf(Collection<? extends T> values)
of(java.util.Collection)
instead.public static <T> Array<T> of(Seq<T> values)
Seq
.values
- the array values.NullPointerException
- if the values
array is null
.@Deprecated public static <T> Array<T> valueOf(Seq<T> values)
of(Seq)
instead.public static Array<Boolean> box(boolean... values)
Array<Boolean>
.values
- the native array to box.public static Array<Character> box(char... values)
Array<Char>
.values
- the native array to box.public static Array<Short> box(short... values)
Array<Short>
.values
- the native array to box.public static Array<Integer> box(int... values)
Array<Integer>
.values
- the native array to box.public static Array<Long> box(long... values)
Array<Long>
.values
- the native array to box.public static Array<Float> box(float... values)
Array<Float>
.values
- the native array to box.public static Array<Double> box(double... values)
Array<Double>
.values
- the native array to box.public static boolean[] unboxBoolean(Array<Boolean> values)
values
- the Array
to unbox.public static char[] unboxChar(Array<Character> values)
values
- the Array
to unbox.public static short[] unboxShort(Array<Short> values)
values
- the Array
to unbox.public static int[] unboxInt(Array<Integer> values)
values
- the Array
to unbox.public static long[] unboxLong(Array<Long> values)
values
- the Array
to unbox.public static float[] unboxFloat(Array<Float> values)
values
- the Array
to unbox.public static double[] unboxDouble(Array<Double> values)
values
- the Array
to unbox.public T get(int index)
Seq
index
.public int indexOf(Object element)
Seq
public int indexOf(Object element, int start)
Seq
public int indexOf(Object element, int start, int end)
Seq
indexOf
in interface Seq<T>
element
- element to search for, can be null
start
- the start index (inclusively) for the element search.end
- the end index (exclusively) for the element search.public int indexWhere(Function<? super T,Boolean> predicate)
Seq
Returns the index of the first element on which the given predicate
returns true
, or -1 if the predicate returns false for every
sequence element.
// Finding index of first null value.
final int index = seq.indexOf(new Predicates.Nil());
// Assert of no null values.
assert (sequence.indexOf(new Predicates.Nil()) == -1);
indexWhere
in interface Seq<T>
predicate
- the search predicate.true
, or -1 if the predicate returns false
for every sequence element.public int indexWhere(Function<? super T,Boolean> predicate, int start)
Seq
Returns the index of the first element on which the given predicate
returns true
, or -1 if the predicate returns false for every
sequence element.
// Finding index of first null value.
final int index = seq.indexOf(new Predicates.Nil());
// Assert of no null values.
assert (sequence.indexOf(new Predicates.Nil()) == -1);
indexWhere
in interface Seq<T>
predicate
- the search predicate.true
, or -1 if the predicate returns false
for every sequence element.public int indexWhere(Function<? super T,Boolean> predicate, int start, int end)
Seq
Returns the index of the first element on which the given predicate
returns true
, or -1 if the predicate returns false for every
sequence element.
// Finding index of first null value.
final int index = seq.indexOf(new Predicates.Nil());
// Assert of no null values.
assert (sequence.indexOf(new Predicates.Nil()) == -1);
indexWhere
in interface Seq<T>
predicate
- the search predicate.true
, or -1 if the predicate returns false
for every sequence element.public int lastIndexOf(Object element)
Seq
lastIndexOf
in interface Seq<T>
element
- element to search for, can be null
public int lastIndexOf(Object element, int end)
Seq
lastIndexOf
in interface Seq<T>
element
- element to search for, can be null
public int lastIndexOf(Object element, int start, int end)
Seq
lastIndexOf
in interface Seq<T>
element
- element to search for, can be null
public int lastIndexWhere(Function<? super T,Boolean> predicate)
Seq
true
, or -1 if the predicate returns false for every
sequence element.lastIndexWhere
in interface Seq<T>
predicate
- the search predicate.true
, or -1 if the predicate returns false for
every sequence element.public int lastIndexWhere(Function<? super T,Boolean> predicate, int end)
Seq
true
, or -1 if the predicate returns false for every
sequence element.lastIndexWhere
in interface Seq<T>
predicate
- the search predicate.true
, or -1 if the predicate returns false for
every sequence element.public int lastIndexWhere(Function<? super T,Boolean> predicate, int start, int end)
Seq
true
, or -1 if the predicate returns false for every
sequence element.lastIndexWhere
in interface Seq<T>
predicate
- the search predicate.true
, or -1 if the predicate returns false for
every sequence element.@Deprecated public <R> void foreach(Function<? super T,? extends R> function)
forEach(Function)
instead.public <R> void forEach(Function<? super T,? extends R> function)
Seq
function
to all elements of this sequence.@Deprecated public boolean forall(Function<? super T,Boolean> predicate)
forAll(Function)
instead.public boolean forAll(Function<? super T,Boolean> predicate)
Seq
public boolean contains(Object element)
Seq
true
if this sequence contains the specified element.public int length()
Seq
public <B> Iterator<B> iterator(Function<? super T,? extends B> converter)
Seq
B
.public Object[] toArray()
Seq
toArray
in interface Seq<T>
Collection.toArray()
public T[] toArray(T[] array)
Seq
toArray
in interface Seq<T>
array
- the array into which the elements of this array are to be
stored, if it is big enough; otherwise, a new array of the same
runtime type is allocated for this purpose.Collection.toArray(Object[])
public int hashCode()
Seq
int hashCode = 1;
final Iterator<E> it = seq.iterator();
while (it.hasNext()) {
final E obj = it.next();
hashCode = 31*hashCode + (obj == null ? 0 : obj.hashCode());
}
hashCode
in interface Seq<T>
hashCode
in class Object
List.hashCode()
public boolean equals(Object obj)
Seq
equals
in interface Seq<T>
equals
in class Object
obj
- the object to be compared for equality with this sequence.true
if the specified object is equal to this sequence,
false
otherwise.List.equals(Object)
public String toString(String prefix, String separator, String suffix)
Seq
public String toString(String separator)
Seq
© 2007-2014 Franz Wilhelmstötter (2014-03-07 19:35)