Package edu.gvsu.kurmasz.warszawa.util
Class ArrayUtils
java.lang.Object
edu.gvsu.kurmasz.warszawa.util.ArrayUtils
public class ArrayUtils
extends java.lang.Object
A set of useful methods for dealing with arrays (different from those in
Java.util.Arrays
).- Author:
- Zachary Kurmas
-
Constructor Summary
Constructors Constructor Description ArrayUtils()
-
Method Summary
Modifier and Type Method Description static void
permute(int[] array)
Randomly permute the values inarray
.static void
permute(int[] array, java.util.Random rnd)
Randomly permute the values inarray
.static <T> void
permute(T[] array)
Randomly permute the values inarray
.static <T> void
permute(T[] array, java.util.Random rnd)
Randomly permute the values inarray
.static int[]
shrinkToFit(int[] array, int size)
Generate and fill a new array with no extra room.static long[]
shrinkToFit(long[] array, int size)
Generate and fill a new array with no extra room.static <T> T[]
shrinkToFit(T[] array, int size)
Generate and fill a new array with no extra room.
-
Constructor Details
-
ArrayUtils
public ArrayUtils()
-
-
Method Details
-
shrinkToFit
public static long[] shrinkToFit(long[] array, int size)Generate and fill a new array with no extra room. Note: For consistency (i.e., having the method always return a new object), a new array is generated, even ifarray.length == size
. Ifarray.length == 0
thenarray
is returned (becausearray
is immutable).- Parameters:
array
- the array to "shrink".size
- the number of elements in the array to keep.- Returns:
- a new array containing the first
size
elements ofarray
. (See note above.) - Throws:
java.lang.IllegalArgumentException
- ifsize > array.length
.
-
shrinkToFit
public static int[] shrinkToFit(int[] array, int size)Generate and fill a new array with no extra room. Note: For consistency (i.e., having the method always return a new object), a new array is generated, even ifarray.length == size
. Ifarray.length == 0
thenarray
is returned (becausearray
is immutable).- Parameters:
array
- the array to "shrink".size
- the number of elements in the array to keep.- Returns:
- a new array containing the first
size
elements ofarray
. (See note above.) - Throws:
java.lang.IllegalArgumentException
- ifsize > array.length
.
-
shrinkToFit
public static <T> T[] shrinkToFit(T[] array, int size)Generate and fill a new array with no extra room. Note: For consistency (i.e., having the method always return a new object), a new array is generated, even ifarray.length == size
. Ifarray.length == 0
thenarray
is returned (becausearray
is immutable).- Parameters:
array
- the array to "shrink".size
- the number of elements in the array to keep.- Returns:
- a new array containing the first
size
elements ofarray
. (See note above.) - Throws:
java.lang.IllegalArgumentException
- ifsize > array.length
.
-
permute
public static <T> void permute(T[] array, java.util.Random rnd)Randomly permute the values inarray
.- Parameters:
array
- the array to permuternd
- thejava.util.Random
object used to determine where values are randomly moved.
-
permute
public static <T> void permute(T[] array)Randomly permute the values inarray
.- Parameters:
array
- the array to permute
-
permute
public static void permute(int[] array, java.util.Random rnd)Randomly permute the values inarray
.- Parameters:
array
- the array to permuternd
- thejava.util.Random
object used to determine where values are randomly moved.
-
permute
public static void permute(int[] array)Randomly permute the values inarray
.- Parameters:
array
- the array to permute
-