public class Array<T> extends Object implements Iterable<T>
Modifier and Type | Class and Description |
---|---|
static class |
Array.ArrayIterable<T> |
static class |
Array.ArrayIterator<T> |
static class |
Array.PredicateIterable<T> |
static class |
Array.PredicateIterator<T> |
Constructor and Description |
---|
Array()
Creates an ordered array with a capacity of 16.
|
Array(Array<? extends T> array)
Creates a new array containing the elements in the specified array.
|
Array(Class arrayType)
Creates an ordered array with
items of the specified type and a capacity of 16. |
Array(Collection<T> collection)
Creates a new ordered array containing the elements of the given collection.
|
Array(int capacity)
Creates an ordered array with the specified capacity.
|
Array(T[] array)
Creates a new ordered array containing the elements in the specified array.
|
Modifier and Type | Method and Description |
---|---|
void |
add(T value) |
void |
addAll(Array<? extends T> array) |
void |
addAll(Array<? extends T> array,
int start,
int count) |
void |
addAll(T... array) |
void |
addAll(T[] array,
int start,
int count) |
void |
clear()
Removes all items.
|
boolean |
containsByEquality(T value) |
boolean |
containsByIdentity(T value) |
static <T> Array<T> |
empty() |
T[] |
ensureCapacity(int additionalCapacity)
Increases the size of the backing array to accommodate the specified number of additional items.
|
boolean |
equals(Object object) |
T |
first() |
T |
get(int index) |
T[] |
getItems() |
int |
hashCode() |
int |
indexOfByEquality(T value) |
int |
indexOfByIdentity(T value) |
void |
insert(int index,
T value) |
boolean |
isEmpty() |
boolean |
isNotEmpty() |
boolean |
isOrdered() |
Iterator<T> |
iterator()
Returns an iterator for the items in the array.
|
T |
last() |
int |
lastIndexOfByEquality(T value) |
int |
lastIndexOfByIdentity(T value) |
static <T> Array<T> |
of(boolean ordered,
int capacity,
Class<T> arrayType) |
static <T> Array<T> |
of(Class<T> arrayType) |
T |
pop()
Removes and returns the last item.
|
T |
random() |
boolean |
removeAllByEquality(Array<? extends T> array)
Removes from this array all of elements contained in the specified array using .equals() to check.
|
boolean |
removeAllByIdentity(Array<? extends T> array)
Removes from this array all of elements contained in the specified array using == to check.
|
T |
removeIndex(int index)
Removes and returns the item at the specified index.
|
void |
removeRange(int start,
int end)
Removes the items between the specified indices, inclusive.
|
boolean |
removeValueByEquality(T value)
Removes the first instance of the specified value in the array.
|
boolean |
removeValueByIdentity(T value)
Removes the first instance of the specified value in the array.
|
protected T[] |
resize(int newSize)
Creates a new backing array with the specified size containing the current items.
|
void |
reverse()
Reverses the items.
|
Iterable<T> |
select(Predicate<T> predicate)
Returns an iterable for the selected items in the array.
|
T |
selectRanked(Comparator<T> comparator,
int kthLowest)
Selects the nth-lowest element from the Array according to Comparator ranking.
|
int |
selectRankedIndex(Comparator<T> comparator,
int kthLowest) |
void |
set(int index,
T value) |
T[] |
setSize(int newSize)
Sets the array size, leaving any values beyond the current size null.
|
T[] |
shrink()
Reduces the size of the backing array to the size of the actual items.
|
void |
shuffle()
Shuffles the items.
|
int |
size() |
void |
sort()
Sorts this array.
|
void |
sort(Comparator<? super T> comparator)
Sorts the array using given comparator.
|
void |
swap(int first,
int second) |
T[] |
toArray()
Returns the items as an array.
|
<V> V[] |
toArray(Class type) |
List<T> |
toList() |
String |
toString() |
String |
toString(String separator) |
void |
truncate(int newSize)
Reduces the size of the array to the specified size.
|
static <T> Array<T> |
with(T... array) |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
forEach, spliterator
public Array()
public Array(int capacity)
capacity
- initial capacitypublic Array(Class arrayType)
items
of the specified type and a capacity of 16.public Array(Array<? extends T> array)
public Array(T[] array)
public Array(Collection<T> collection)
collection
- the collection to take elements frompublic T[] getItems()
public int size()
public boolean isEmpty()
public boolean isNotEmpty()
public boolean isOrdered()
public void add(T value)
public void addAll(T... array)
public void addAll(T[] array, int start, int count)
public T get(int index)
public void set(int index, T value)
public void insert(int index, T value)
public void swap(int first, int second)
public boolean containsByIdentity(T value)
value
- May be null, the identity "==" comparison is usedpublic boolean containsByEquality(T value)
value
- May be null, the equality ".equals" comparison is usedpublic int indexOfByIdentity(T value)
value
- May be null, the identity "==" comparison is usedpublic int indexOfByEquality(T value)
value
- May be null, the equality ".equals" comparison is usedpublic int lastIndexOfByIdentity(T value)
value
- May be null, the identity "==" comparison is usedpublic int lastIndexOfByEquality(T value)
value
- May be null, the equality ".equals" comparison is usedpublic boolean removeValueByIdentity(T value)
value
- the value to compare against using identity (==)public boolean removeValueByEquality(T value)
value
- the value to compare against using equality (.equals())public T removeIndex(int index)
index
- the index of the element to removepublic void removeRange(int start, int end)
start
- start indexend
- end indexpublic boolean removeAllByIdentity(Array<? extends T> array)
array
- given arraypublic boolean removeAllByEquality(Array<? extends T> array)
array
- given arraypublic T pop()
public T last()
public T first()
public void clear()
public T[] shrink()
public T[] ensureCapacity(int additionalCapacity)
additionalCapacity
- extra capacitypublic T[] setSize(int newSize)
newSize
- new array sizeprotected T[] resize(int newSize)
newSize
- new array sizepublic void truncate(int newSize)
newSize
- new array sizepublic void sort()
Comparable
.
This method is not thread safe (uses Sort.instance()
).public void sort(Comparator<? super T> comparator)
Sort.instance()
).comparator
- comparator for sortingpublic void reverse()
public void shuffle()
public Iterator<T> iterator()
Array.ArrayIterator
constructor for nested or multithreaded iteration.public Iterable<T> select(Predicate<T> predicate)
Array.PredicateIterable
constructor for nested or multithreaded iteration.predicate
- predicate for selectionpublic T selectRanked(Comparator<T> comparator, int kthLowest)
IllegalArgumentException
will be thrown.comparator
- used for comparisonkthLowest
- rank of desired object according to comparison, n is based on ordinal numbers, not array indices. for min
value use 1, for max value use size of array, using 0 results in runtime exception.Select
public int selectRankedIndex(Comparator<T> comparator, int kthLowest)
comparator
- used for comparisonkthLowest
- rank of desired object according to comparison, n is based on ordinal numbers, not array indices. for min
value use 1, for max value use size of array, using 0 results in runtime exception.selectRanked(Comparator, int)
public T random()
public T[] toArray()
Array(Class)
constructor must have been used.
Otherwise use toArray(Class)
to specify the array type.public <V> V[] toArray(Class type)
public static <T> Array<T> of(Class<T> arrayType)
Array(Class)
public static <T> Array<T> of(boolean ordered, int capacity, Class<T> arrayType)
Array(boolean, int, Class)
public static <T> Array<T> with(T... array)
Array(Object[])
public static <T> Array<T> empty()
Copyright © 2018. All rights reserved.