public class ArrayTools
extends java.lang.Object
OrderedMap
, OrderedSet
, K2
,
and similar ordered collections plus ArrayList using reorder(ArrayList, int...)
in this class.
Created by Tommy Ettinger on 11/17/2016.Constructor and Description |
---|
ArrayTools() |
Modifier and Type | Method and Description |
---|---|
static char[] |
charSpan(char start,
char end)
Stupidly simple convenience method that produces a char range from start to end, including end, as a char array.
|
static boolean[][] |
copy(boolean[][] source)
Gets a copy of the 2D boolean array, source, that has the same data but shares no references with source.
|
static char[][] |
copy(char[][] source)
Gets a copy of the 2D char array, source, that has the same data but shares no references with source.
|
static double[][] |
copy(double[][] source)
Gets a copy of the 2D double array, source, that has the same data but shares no references with source.
|
static float[][] |
copy(float[][] source)
Gets a copy of the 2D float array, source, that has the same data but shares no references with source.
|
static int[][] |
copy(int[][] source)
Gets a copy of the 2D int array, source, that has the same data but shares no references with source.
|
static void |
fill(boolean[][] array2d,
boolean value)
Fills
array2d with value . |
static void |
fill(boolean[][] array2d,
boolean value,
int startX,
int startY,
int endX,
int endY)
Fills a sub-section of
array2d with value , with the section defined by start/end x/y. |
static boolean[][] |
fill(boolean contents,
int width,
int height)
Creates a 2D array of the given width and height, filled with entirely with the value contents.
|
static void |
fill(byte[][] array2d,
byte value)
Fills
array2d with value . |
static byte[][] |
fill(byte contents,
int width,
int height)
Creates a 2D array of the given width and height, filled with entirely with the value contents.
|
static void |
fill(char[][] array2d,
char value)
Fills
array2d with value . |
static void |
fill(char[][] array2d,
char value,
int startX,
int startY,
int endX,
int endY)
Fills a sub-section of
array2d with value , with the section defined by start/end x/y. |
static char[][] |
fill(char contents,
int width,
int height)
Creates a 2D array of the given width and height, filled with entirely with the value contents.
|
static void |
fill(double[][][] array3d,
double value)
Fills
array3d with value . |
static void |
fill(double[][] array2d,
double value)
Fills
array2d with value . |
static void |
fill(double[][] array2d,
double value,
int startX,
int startY,
int endX,
int endY)
Fills a sub-section of
array2d with value , with the section defined by start/end x/y. |
static double[][] |
fill(double contents,
int width,
int height)
Creates a 2D array of the given width and height, filled with entirely with the value contents.
|
static void |
fill(float[][] array2d,
float value)
Fills
array2d with value . |
static void |
fill(float[][] array2d,
float value,
int startX,
int startY,
int endX,
int endY)
Fills a sub-section of
array2d with value , with the section defined by start/end x/y. |
static float[][] |
fill(float contents,
int width,
int height)
Creates a 2D array of the given width and height, filled with entirely with the value contents.
|
static void |
fill(int[][] array2d,
int value)
Fills
array2d with value . |
static void |
fill(int[][] array2d,
int value,
int startX,
int startY,
int endX,
int endY)
Fills a sub-section of
array2d with value , with the section defined by start/end x/y. |
static int[][] |
fill(int contents,
int width,
int height)
Creates a 2D array of the given width and height, filled with entirely with the value contents.
|
static boolean[][] |
insert(boolean[][] source,
boolean[][] target,
int x,
int y)
Inserts as much of source into target at the given x,y position as target can hold or source can supply.
|
static char[][] |
insert(char[][] source,
char[][] target,
int x,
int y)
Inserts as much of source into target at the given x,y position as target can hold or source can supply.
|
static double[][] |
insert(double[][] source,
double[][] target,
int x,
int y)
Inserts as much of source into target at the given x,y position as target can hold or source can supply.
|
static float[][] |
insert(float[][] source,
float[][] target,
int x,
int y)
Inserts as much of source into target at the given x,y position as target can hold or source can supply.
|
static int[][] |
insert(int[][] source,
int[][] target,
int x,
int y)
Inserts as much of source into target at the given x,y position as target can hold or source can supply.
|
static int[] |
invertOrdering(int[] ordering)
Given an ordering such as one produced by
RNG.randomOrdering(int, int[]) , this finds
its inverse, able to reverse the reordering and vice versa. |
static int[] |
invertOrdering(int[] ordering,
int[] dest)
Given an ordering such as one produced by
RNG.randomOrdering(int, int[]) , this finds
its inverse, able to reverse the reordering and vice versa. |
static char |
letterAt(int index)
Gets the nth letter from the set that SquidLib is likely to support; from index 0 (returning 'A') to 255
(returning the Greek lower-case letter gamma, 'γ') and wrapping around if given negative numbers or numbers
larger than 255.
|
static char[] |
letterSpan(int charCount)
Stupidly simple convenience method that produces a char array containing only letters that can be reasonably
displayed (with SquidLib's default text display assets, at least).
|
static void |
randomFill(char[][] array2d,
char[] values,
long seed)
Randomly fills all of
array2d with random values generated from seed , choosing chars to place in
the given 2D array by selecting them at random from the given 1D char array values . |
static void |
randomFill(char[][] array2d,
java.lang.CharSequence values,
long seed)
Randomly fills all of
array2d with random values generated from seed , choosing chars to place in
the given 2D array by selecting them at random from the given 1D char array values . |
static void |
randomFill(double[][] array2d,
double bound,
long seed)
Randomly fills all of
array2d with random values generated from seed , limiting results to between
0 and bound , exclusive. |
static void |
randomFill(double[][] array2d,
long seed)
Randomly fills all of
array2d with random values generated from seed ; can fill an element with
any double between 0.0 inclusive and 1.0 exclusive. |
static void |
randomFill(float[][] array2d,
float bound,
long seed)
Randomly fills all of
array2d with random values generated from seed , limiting results to between
0 and bound , exclusive. |
static void |
randomFill(float[][] array2d,
long seed)
Randomly fills all of
array2d with random values generated from seed ; can fill an element with
any float between 0.0 inclusive and 1.0 exclusive. |
static void |
randomFill(int[][] array2d,
int bound,
long seed)
Randomly fills all of
array2d with random values generated from seed , limiting results to between
0 and bound , exclusive. |
static void |
randomFill(int[][] array2d,
long seed)
Randomly fills all of
array2d with random values generated from seed ; can fill an element with
any int, positive or negative. |
static void |
randomFill(long[][] array2d,
long seed)
Randomly fills all of
array2d with random values generated from seed ; can fill an element with
any long, positive or negative. |
static int[] |
range(int end)
Stupidly simple convenience method that produces a range from 0 to end, not including end, as an int array.
|
static int[] |
range(int start,
int end)
Stupidly simple convenience method that produces a range from start to end, not including end, as an int array.
|
static <T> java.util.ArrayList<T> |
reorder(java.util.ArrayList<T> list,
int... ordering)
Rearranges an ArrayList to use the given ordering, returning a copy; random orderings can be produced with
RNG.randomOrdering(int) or
RNG.randomOrdering(int, int[]) . |
static boolean[] |
reverse(boolean[] data)
Reverses the array given as a parameter, in-place, and returns the modified original.
|
static byte[] |
reverse(byte[] data)
Reverses the array given as a parameter, in-place, and returns the modified original.
|
static char[] |
reverse(char[] data)
Reverses the array given as a parameter, in-place, and returns the modified original.
|
static double[] |
reverse(double[] data)
Reverses the array given as a parameter, in-place, and returns the modified original.
|
static float[] |
reverse(float[] data)
Reverses the array given as a parameter, in-place, and returns the modified original.
|
static int[] |
reverse(int[] data)
Reverses the array given as a parameter, in-place, and returns the modified original.
|
static <T> T[] |
reverse(T[] data)
Reverses the array given as a parameter, in-place, and returns the modified original.
|
public static int[] range(int end)
end
- the exclusive upper bound on the rangepublic static int[] range(int start, int end)
start
- the inclusive lower bound on the rangeend
- the exclusive upper bound on the rangepublic static char[] charSpan(char start, char end)
start
- the inclusive lower bound on the range, such as 'a'end
- the inclusive upper bound on the range, such as 'z'public static char[] letterSpan(int charCount)
charSpan(char, char)
.
This set does not contain "visual duplicate" letters, such as Latin alphabet capital letter 'A' and Greek
alphabet capital letter alpha, 'Α'; it does contain many accented Latin letters and the visually-distinct Greek
letters, up to a point.charCount
- the number of letters to return in an array; the maximum this will produce is 256public static char letterAt(int index)
index
- typically from 0 to 255, but all ints are allowed and will produce letterspublic static char[][] copy(char[][] source)
source
- a 2D char arraypublic static double[][] copy(double[][] source)
source
- a 2D double arraypublic static float[][] copy(float[][] source)
source
- a 2D float arraypublic static int[][] copy(int[][] source)
source
- a 2D int arraypublic static boolean[][] copy(boolean[][] source)
source
- a 2D boolean arraypublic static char[][] insert(char[][] source, char[][] target, int x, int y)
source
- a 2D char array that will be copied and inserted into targettarget
- a 2D char array that will be modified by receiving as much of source as it can holdx
- the x position in target to receive the items from the first cell in sourcey
- the y position in target to receive the items from the first cell in sourcepublic static double[][] insert(double[][] source, double[][] target, int x, int y)
source
- a 2D double array that will be copied and inserted into targettarget
- a 2D double array that will be modified by receiving as much of source as it can holdx
- the x position in target to receive the items from the first cell in sourcey
- the y position in target to receive the items from the first cell in sourcepublic static float[][] insert(float[][] source, float[][] target, int x, int y)
source
- a 2D float array that will be copied and inserted into targettarget
- a 2D float array that will be modified by receiving as much of source as it can holdx
- the x position in target to receive the items from the first cell in sourcey
- the y position in target to receive the items from the first cell in sourcepublic static int[][] insert(int[][] source, int[][] target, int x, int y)
source
- a 2D int array that will be copied and inserted into targettarget
- a 2D int array that will be modified by receiving as much of source as it can holdx
- the x position in target to receive the items from the first cell in sourcey
- the y position in target to receive the items from the first cell in sourcepublic static boolean[][] insert(boolean[][] source, boolean[][] target, int x, int y)
source
- a 2D boolean array that will be copied and inserted into targettarget
- a 2D boolean array that will be modified by receiving as much of source as it can holdx
- the x position in target to receive the items from the first cell in sourcey
- the y position in target to receive the items from the first cell in sourcepublic static char[][] fill(char contents, int width, int height)
fill(char[][], char)
to modify an existing 2D array instead.contents
- the value to fill the array withwidth
- the desired widthheight
- the desired heightpublic static float[][] fill(float contents, int width, int height)
fill(float[][], float)
to modify an existing 2D array instead.contents
- the value to fill the array withwidth
- the desired widthheight
- the desired heightpublic static double[][] fill(double contents, int width, int height)
fill(double[][], double)
to modify an existing 2D array instead.contents
- the value to fill the array withwidth
- the desired widthheight
- the desired heightpublic static int[][] fill(int contents, int width, int height)
fill(int[][], int)
to modify an existing 2D array instead.contents
- the value to fill the array withwidth
- the desired widthheight
- the desired heightpublic static byte[][] fill(byte contents, int width, int height)
fill(byte[][], byte)
to modify an existing 2D array instead.contents
- the value to fill the array withwidth
- the desired widthheight
- the desired heightpublic static boolean[][] fill(boolean contents, int width, int height)
fill(boolean[][], boolean)
to modify an existing 2D array instead.contents
- the value to fill the array withwidth
- the desired widthheight
- the desired heightpublic static void fill(boolean[][] array2d, boolean value)
array2d
with value
.
Not to be confused with fill(boolean, int, int)
, which makes a new 2D array.array2d
- a 2D array that will be modified in-placevalue
- the value to fill all of array2D withpublic static void fill(char[][] array2d, char value)
array2d
- a 2D array that will be modified in-placevalue
- the value to fill all of array2D withpublic static void fill(float[][] array2d, float value)
array2d
with value
.
Not to be confused with fill(float, int, int)
, which makes a new 2D array.array2d
- a 2D array that will be modified in-placevalue
- the value to fill all of array2D withpublic static void fill(double[][] array2d, double value)
array2d
with value
.
Not to be confused with fill(double, int, int)
, which makes a new 2D array.array2d
- a 2D array that will be modified in-placevalue
- the value to fill all of array2D withpublic static void fill(double[][][] array3d, double value)
array3d
with value
.
Not to be confused with fill(double[][], double)
, which fills a 2D array instead of a 3D one, or with
fill(double, int, int)
, which makes a new 2D array.array3d
- a 3D array that will be modified in-placevalue
- the value to fill all of array3d withpublic static void fill(int[][] array2d, int value)
array2d
- a 2D array that will be modified in-placevalue
- the value to fill all of array2D withpublic static void fill(byte[][] array2d, byte value)
array2d
- a 2D array that will be modified in-placevalue
- the value to fill all of array2D withpublic static void fill(boolean[][] array2d, boolean value, int startX, int startY, int endX, int endY)
array2d
with value
, with the section defined by start/end x/y.
Not to be confused with fill(boolean, int, int)
, which makes a new 2D array.array2d
- a 2D array that will be modified in-placevalue
- the value to fill all of array2D withstartX
- the first x position to fill (inclusive)startY
- the first y position to fill (inclusive)endX
- the last x position to fill (inclusive)endY
- the last y position to fill (inclusive)public static void fill(char[][] array2d, char value, int startX, int startY, int endX, int endY)
array2d
with value
, with the section defined by start/end x/y.
Not to be confused with fill(char, int, int)
, which makes a new 2D array.array2d
- a 2D array that will be modified in-placevalue
- the value to fill all of array2D withstartX
- the first x position to fill (inclusive)startY
- the first y position to fill (inclusive)endX
- the last x position to fill (inclusive)endY
- the last y position to fill (inclusive)public static void fill(float[][] array2d, float value, int startX, int startY, int endX, int endY)
array2d
with value
, with the section defined by start/end x/y.
Not to be confused with fill(float, int, int)
, which makes a new 2D array.array2d
- a 2D array that will be modified in-placevalue
- the value to fill all of array2D withstartX
- the first x position to fill (inclusive)startY
- the first y position to fill (inclusive)endX
- the last x position to fill (inclusive)endY
- the last y position to fill (inclusive)public static void fill(double[][] array2d, double value, int startX, int startY, int endX, int endY)
array2d
with value
, with the section defined by start/end x/y.
Not to be confused with fill(double, int, int)
, which makes a new 2D array.array2d
- a 2D array that will be modified in-placevalue
- the value to fill all of array2D withstartX
- the first x position to fill (inclusive)startY
- the first y position to fill (inclusive)endX
- the last x position to fill (inclusive)endY
- the last y position to fill (inclusive)public static void fill(int[][] array2d, int value, int startX, int startY, int endX, int endY)
array2d
with value
, with the section defined by start/end x/y.
Not to be confused with fill(int, int, int)
, which makes a new 2D array.array2d
- a 2D array that will be modified in-placevalue
- the value to fill all of array2D withstartX
- the first x position to fill (inclusive)startY
- the first y position to fill (inclusive)endX
- the last x position to fill (inclusive)endY
- the last y position to fill (inclusive)public static void randomFill(long[][] array2d, long seed)
array2d
with random values generated from seed
; can fill an element with
any long, positive or negative.
Fairly efficient; uses a fast random number generation algorithm that can avoid some unnecessary work in this
context, and improves quality by seeding each column differently. Generates (height + 1) * width
random
values to fill the height * width
elements in array2d.array2d
- a 2D array that will be modified in-placeseed
- the seed for the random values, as a longpublic static void randomFill(int[][] array2d, long seed)
array2d
with random values generated from seed
; can fill an element with
any int, positive or negative.
Fairly efficient; uses a fast random number generation algorithm that can avoid some unnecessary work in this
context, and improves quality by seeding each column differently. Generates (height + 1) * width
random
values to fill the height * width
elements in array2d.array2d
- a 2D array that will be modified in-placeseed
- the seed for the random values, as a longpublic static void randomFill(int[][] array2d, int bound, long seed)
array2d
with random values generated from seed
, limiting results to between
0 and bound
, exclusive.
Fairly efficient; uses a fast random number generation algorithm that can avoid some unnecessary work in this
context, and improves quality by seeding each column differently. Generates (height + 1) * width
random
values to fill the height * width
elements in array2d.array2d
- a 2D array that will be modified in-placebound
- the upper exclusive limit for the ints this can produceseed
- the seed for the random values, as a longpublic static void randomFill(char[][] array2d, char[] values, long seed)
array2d
with random values generated from seed
, choosing chars to place in
the given 2D array by selecting them at random from the given 1D char array values
.
Fairly efficient; uses a fast random number generation algorithm that can avoid some unnecessary work in this
context, and improves quality by seeding each column differently. Generates (height + 1) * width
random
values to fill the height * width
elements in array2d.array2d
- a 2D array that will be modified in-placevalues
- a 1D char array containing the possible char values that can be chosen to fill array2dseed
- the seed for the random values, as a longpublic static void randomFill(char[][] array2d, java.lang.CharSequence values, long seed)
array2d
with random values generated from seed
, choosing chars to place in
the given 2D array by selecting them at random from the given 1D char array values
.
Fairly efficient; uses a fast random number generation algorithm that can avoid some unnecessary work in this
context, and improves quality by seeding each column differently. Generates (height + 1) * width
random
values to fill the height * width
elements in array2d.array2d
- a 2D array that will be modified in-placevalues
- a 1D char array containing the possible char values that can be chosen to fill array2dseed
- the seed for the random values, as a longpublic static void randomFill(float[][] array2d, long seed)
array2d
with random values generated from seed
; can fill an element with
any float between 0.0 inclusive and 1.0 exclusive.
Fairly efficient; uses a fast random number generation algorithm that can avoid some unnecessary work in this
context, and improves quality by seeding each column differently. Generates (height + 1) * width
random
values to fill the height * width
elements in array2d.array2d
- a 2D array that will be modified in-placeseed
- the seed for the random values, as a longpublic static void randomFill(float[][] array2d, float bound, long seed)
array2d
with random values generated from seed
, limiting results to between
0 and bound
, exclusive.
Fairly efficient; uses a fast random number generation algorithm that can avoid some unnecessary work in this
context, and improves quality by seeding each column differently. Generates (height + 1) * width
random
values to fill the height * width
elements in array2d.array2d
- a 2D array that will be modified in-placebound
- the upper exclusive limit for the floats this can produceseed
- the seed for the random values, as a longpublic static void randomFill(double[][] array2d, long seed)
array2d
with random values generated from seed
; can fill an element with
any double between 0.0 inclusive and 1.0 exclusive.
Fairly efficient; uses a fast random number generation algorithm that can avoid some unnecessary work in this
context, and improves quality by seeding each column differently. Generates (height + 1) * width
random
values to fill the height * width
elements in array2d.array2d
- a 2D array that will be modified in-placeseed
- the seed for the random values, as a longpublic static void randomFill(double[][] array2d, double bound, long seed)
array2d
with random values generated from seed
, limiting results to between
0 and bound
, exclusive.
Fairly efficient; uses a fast random number generation algorithm that can avoid some unnecessary work in this
context, and improves quality by seeding each column differently. Generates (height + 1) * width
random
values to fill the height * width
elements in array2d.array2d
- a 2D array that will be modified in-placebound
- the upper exclusive limit for the doubles this can produceseed
- the seed for the random values, as a longpublic static <T> java.util.ArrayList<T> reorder(java.util.ArrayList<T> list, int... ordering)
RNG.randomOrdering(int)
or
RNG.randomOrdering(int, int[])
. These orderings will never repeat an earlier element,
and the returned ArrayList may be shorter than the original if ordering
isn't as long as list
.
Using a random ordering is like shuffling, but allows you to repeat the shuffle exactly on other collections of
the same size. A reordering can also be inverted with invertOrdering(int[])
or
invertOrdering(int[], int[])
, getting the change that will undo another ordering.T
- any generic typelist
- an ArrayList that you want a reordered version of; will not be modified.ordering
- an ordering, typically produced by one of RNG's randomOrdering methods.list
with its ordering changed to match ordering
.public static int[] invertOrdering(int[] ordering)
RNG.randomOrdering(int, int[])
, this finds
its inverse, able to reverse the reordering and vice versa.ordering
- the ordering to find the inverse forpublic static int[] invertOrdering(int[] ordering, int[] dest)
RNG.randomOrdering(int, int[])
, this finds
its inverse, able to reverse the reordering and vice versa. This overload doesn't allocate a new int
array, and instead relies on having an int array of the same size as ordering passed to it as an
additional argument.ordering
- the ordering to find the inverse fordest
- the int array to put the inverse reordering into; should have the same length as orderingpublic static boolean[] reverse(boolean[] data)
data
- an array that will be reversed in-placepublic static char[] reverse(char[] data)
data
- an array that will be reversed in-placepublic static float[] reverse(float[] data)
data
- an array that will be reversed in-placepublic static double[] reverse(double[] data)
data
- an array that will be reversed in-placepublic static int[] reverse(int[] data)
data
- an array that will be reversed in-placepublic static byte[] reverse(byte[] data)
data
- an array that will be reversed in-placepublic static <T> T[] reverse(T[] data)
data
- an array that will be reversed in-placeCopyright © Eben Howard 2012–2022. All rights reserved.