public final class IntArrayList
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
protected static int |
DEFAULT_SIZE
The standard starting point for the array length
|
protected int |
numElements
The number of used elements in this
|
Constructor and Description |
---|
IntArrayList()
Initializes an empty vector with a default allocation.
|
IntArrayList(int startSize)
Initializes an empty vector (allocating startSize locations for
additions).
|
IntArrayList(IntArrayList copy)
Copy constructor
|
Modifier and Type | Method and Description |
---|---|
int |
add(int element)
Adds element to the end of the vector (allocating more space as needed
|
protected void |
checkBounds(int idx)
Checks that idx is in the allowed elements of the vector (distinct from
the space in elements).
|
protected static void |
checkNewSizePositive(int size)
Helper that ensures the input size is positive
|
void |
clear()
Quickly "clears" the contents (sets numElements to 0).
|
void |
decreaseTo(int size)
Effectively remove elements from the vector.
|
protected void |
ensureNotFull()
Simple helper that increases the size if the current vector is full
|
boolean |
equals(java.lang.Object o) |
int |
get(int idx)
Returns the element at idx
|
int |
hashCode() |
void |
increaseTo(int size)
Resizes the background array if it is too small.
|
protected int |
length()
Returns the number of elements in the vector
|
void |
plusEquals(int idx,
int val)
Adds val to the element at idx
|
void |
randomizeOrder()
Takes the elements of this and alters their order to a random order
(using a variant of Fisher-Yates shuffle called Durstenfeld shuffle).
|
void |
randomizeOrder(java.util.Random r)
Takes the elements of this and alters their order to a random order
(using a variant of Fisher-Yates shuffle called Durstenfeld shuffle).
|
static IntArrayList |
range(int max)
Creates a new instance pre-loaded with values [0 ..
|
static IntArrayList |
range(int min,
int max)
Creates a new instance pre-loaded with values [min ..
|
void |
reserve(int size)
Resizes the background array to the specified size.
|
protected void |
resize(int size)
Replace the internal storage with a new buffer.
|
void |
set(int idx,
int val)
Sets the element at idx
|
void |
shrinkToFit()
Resizes the background array to exactly fit the current number of
elements.
|
int |
size()
Returns the number of elements added to the vector (not the size of the
storage)
|
void |
swap(int p1,
int p2)
Swaps the values stored at p1 and p2.
|
static IntArrayList |
zeros(int size)
Factory method that initializes a IntVector of the input size with zeros
in every entry.
|
protected int numElements
protected static final int DEFAULT_SIZE
public IntArrayList()
public IntArrayList(int startSize)
startSize
- The number of positions to start the storagepublic IntArrayList(IntArrayList copy)
copy
- The vector to make a deep copy ofpublic int add(int element)
element
- The element to add to the endpublic int get(int idx)
idx
- The indexjava.lang.ArrayIndexOutOfBoundsException
- if idx is outside the allowed
boundspublic void set(int idx, int val)
idx
- The indexval
- The new value at idxjava.lang.ArrayIndexOutOfBoundsException
- if idx is outside the allowed
boundspublic void plusEquals(int idx, int val)
idx
- The indexval
- The value to add at idxjava.lang.ArrayIndexOutOfBoundsException
- if idx is outside the allowed
boundspublic void swap(int p1, int p2)
p1
- The first index to swapp2
- The second index to swapjava.lang.ArrayIndexOutOfBoundsException
- if either index is outside the
allowed boundsprotected void resize(int size)
size
- the number of elements in the new internal arrayprotected int length()
public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
@PublicationReference(type=WebPage, title="Fisher-Yates Suffle: The modern algorithm", year=2016, url="https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm", author="Wikipedia") public void randomizeOrder()
public void randomizeOrder(java.util.Random r)
r
- The random number generator to be used herepublic static IntArrayList range(int max)
max
- The number of elements to include in the resultpublic static IntArrayList range(int min, int max)
min
- The number to start the range withmax
- The number one shy of the maximum valuepublic static IntArrayList zeros(int size)
size
- The number of zeros to put in the returned DoubleVectorpublic void reserve(int size)
size
- The new size for the arraypublic void shrinkToFit()
public void increaseTo(int size)
size
- The new sizepublic void decreaseTo(int size)
size
- The new length of the arraypublic void clear()
public int size()
protected void ensureNotFull()
protected void checkBounds(int idx)
idx
- the index into the vectorjava.lang.ArrayIndexOutOfBoundsException
- if idx is outside the allowed
boundsprotected static void checkNewSizePositive(int size)
size
- The new sizejava.lang.IllegalArgumentException
- if size is negative