AnyLogic
Expand
Font size

public final class UtilitiesArray
extends Object
This class provides a lot of commonly used functions for operations with arrays
Author:
AnyLogic North America, LLC https://anylogic.com

Method Summary

Modifier and TypeMethodDescription
static booleanarrayContains(double[] array, double value)
Returns true if the array contains the given value.
The result is the same as from expression: indexOf( array, value ) >= 0
static booleanarrayContains(int[] array, int value)
Returns true if the array contains the given value.
The result is the same as from expression: indexOf( array, value ) >= 0
static booleanarrayContains(Object[] array, Object object)
Returns true if the array contains the given object.
Objects are compared using .equals() method.
Arrays of any object type are supported, e.g.:
static <T> T[]concatenateArrays(T[] a, Collection<? extends T> b)
Concatenates specified array with elements from the given collection.
Result is new array with contents of a followed by all other elements (b)
static <T> T[]concatenateArrays(T[] a, T... b)
Concatenates specified array with the given elements.
Result is new array with contents of a followed by all other elements (b)
static <T> T[]concatenateArrays(T[] a, T[]... b)
Concatenates specified array with the given elements.
Result is new array with contents of a followed by all other elements (b)
static intindexOf(double[] array, double value)
Returns the index of the first occurrence of the given value in the array.
Returns -1 if value not found or if passed array is null or empty.
static intindexOf(double[] array, DoublePredicate test)
Returns the index of the first occurrence of the matching value in the array.
Returns -1 if no matching value is found or if passed array is null or empty.
static intindexOf(int[] array, int value)
Returns the index of the first occurrence of the given value in the array.
Returns -1 if value not found or if passed array is null or empty.
static intindexOf(int[] array, IntPredicate test)
Returns the index of the first occurrence of the matching value in the array.
Returns -1 if no matching value is found or if passed array is null or empty.
static intindexOf(Object[] array, Object object)
Returns the index of the first occurrence of the given object in the array.
Objects are compared using .equals() method.
Returns -1 if value not found or if passed array is null or empty.
Arrays of any object type are supported, e.g.:
static <T> intindexOf(T[] array, Predicate<? super T> test)
Returns the index of the first occurrence of the matching object in the array.
Returns -1 if value not found or if passed array is null or empty.
Arrays of any object type are supported, e.g.:
static intindexOfMax(double[] array)
Returns the index of the maximum value from the given array.
static intindexOfMax(int[] array)
Returns the index of the maximum value from the given array.
static intindexOfMin(double[] array)
Returns the index of the minimum value from the given array.
static intindexOfMin(int[] array)
Returns the index of the minimum value from the given array.
static doublemax(double[] array)
Returns the maximum value from the given array.
Returns Double.NaN if array contains only Double.NaN values.
Throws error if passed array is null or empty.
static intmax(int[] array)
Returns the maximum value from the given array.
Throws error if passed array is null or empty.
static doublemin(double[] array)
Returns the minimum value from the given array.
Returns Double.NaN if array contains only Double.NaN values.
Throws error if passed array is null or empty.
static intmin(int[] array)
Returns the minimum value from the given array.
Throws error if passed array is null or empty.
static <T> T[]toArray(T e, T... b)
Creates array using concatenation of the specified element with given array/elements.

Methods inherited from class java.lang.Object

equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Method Details

min

public static int min(int[] array)
Returns the minimum value from the given array.
Throws error if passed array is null or empty.
Parameters:
array - the array to scan
Returns:
the minimum value from the array

min

public static double min(double[] array)
Returns the minimum value from the given array.
Returns Double.NaN if array contains only Double.NaN values.
Throws error if passed array is null or empty.
Parameters:
array - the array to scan
Returns:
the minimum value from the array

max

public static int max(int[] array)
Returns the maximum value from the given array.
Throws error if passed array is null or empty.
Parameters:
array - the array to scan
Returns:
the maximum value from the array

max

public static double max(double[] array)
Returns the maximum value from the given array.
Returns Double.NaN if array contains only Double.NaN values.
Throws error if passed array is null or empty.
Parameters:
array - the array to scan
Returns:
the maximum value from the array

indexOfMin

public static int indexOfMin(int[] array)
Returns the index of the minimum value from the given array. If there are several such values, returns the smallest index.
Returns -1 if passed array is null or empty.
Parameters:
array - the array to scan
Returns:
the index of the minimum value from the array, starting from 0

indexOfMin

public static int indexOfMin(double[] array)
Returns the index of the minimum value from the given array. If there are several such values, returns the smallest index.
Returns -1 if passed array is null or empty or contains only NaN values.
Parameters:
array - the array to scan
Returns:
the index of the minimum value from the array, starting from 0

indexOfMax

public static int indexOfMax(int[] array)
Returns the index of the maximum value from the given array. If there are several such values, returns the smallest index.
Returns -1 if passed array is null or empty.
Parameters:
array - the array to scan
Returns:
the index of the maximum value from the array, starting from 0

indexOfMax

public static int indexOfMax(double[] array)
Returns the index of the maximum value from the given array. If there are several such values, returns the smallest index.
Returns -1 if passed array is null or empty or contains only NaN values.
Parameters:
array - the array to scan
Returns:
the index of the maximum value from the array, starting from 0

indexOf

public static int indexOf(int[] array,
 int value)
Returns the index of the first occurrence of the given value in the array.
Returns -1 if value not found or if passed array is null or empty.
Parameters:
array - the array to scan
value - the value to search for
Returns:
the index of the value in the array, starting from 0

indexOf

public static int indexOf(int[] array,
 IntPredicate test)
Returns the index of the first occurrence of the matching value in the array.
Returns -1 if no matching value is found or if passed array is null or empty.
Parameters:
array - the array to scan
test - the text expression, e.g. v -> v > 10
Returns:
the index of the value in the array, starting from 0
Since:
8.4

indexOf

public static int indexOf(double[] array,
 double value)
Returns the index of the first occurrence of the given value in the array.
Returns -1 if value not found or if passed array is null or empty.
Parameters:
array - the array to scan
value - the value to search for
Returns:
the index of the value in the array, starting from 0

indexOf

public static int indexOf(double[] array,
 DoublePredicate test)
Returns the index of the first occurrence of the matching value in the array.
Returns -1 if no matching value is found or if passed array is null or empty.
Parameters:
array - the array to scan
test - the text expression, e.g. v -> v > 10
Returns:
the index of the value in the array, starting from 0
Since:
8.4

indexOf

public static int indexOf(Object[] array,
 Object object)
Returns the index of the first occurrence of the given object in the array.
Objects are compared using .equals() method.
Returns -1 if value not found or if passed array is null or empty.
Arrays of any object type are supported, e.g.:
 String[] s = new String[]{ "a", "b", "c" };
 traceln( indexOf( s, "b" ) ); // will print out '1'
Parameters:
array - the array to scan
object - the object to search for, may be null - in this case the method will try to find the index of null in the given array.
Returns:
the index of the object in the array, starting from 0

indexOf

public static <T> int indexOf(T[] array,
 Predicate<? super T> test)
Returns the index of the first occurrence of the matching object in the array.
Returns -1 if value not found or if passed array is null or empty.
Arrays of any object type are supported, e.g.:
 String[] s = new String[]{ "a", "ab", "abc" };
 traceln( indexOf( s, s -> s.length() > 1 ) ); // will print out '1'
Parameters:
array - the array to scan
test - the text expression, special behavior: if null is passed instead of predicate, the method will try to find the index of null in the given array, to be conforming with indexOf(Object[], Object)
Returns:
the index of the matching object in the array, starting from 0
Since:
8.4

arrayContains

public static boolean arrayContains(int[] array,
 int value)
Returns true if the array contains the given value.
The result is the same as from expression: indexOf( array, value ) >= 0
Parameters:
array - the array to scan
value - the value to search for
Returns:
true if the array contains value, false otherwise

arrayContains

public static boolean arrayContains(double[] array,
 double value)
Returns true if the array contains the given value.
The result is the same as from expression: indexOf( array, value ) >= 0
Parameters:
array - the array to scan
value - the value to search for
Returns:
true if the array contains value, false otherwise

arrayContains

public static boolean arrayContains(Object[] array,
 Object object)
Returns true if the array contains the given object.
Objects are compared using .equals() method.
Arrays of any object type are supported, e.g.:
 String[] s = new String[]{ "a", "b", "c" };
 traceln( contains( s, "b" ) ); // will print out 'true'

The result is the same as from expression: indexOf( array, object ) >= 0
Parameters:
array - the array to scan
object - the object to search for
Returns:
true if the array contains object, false otherwise

concatenateArrays

public static <T> T[] concatenateArrays(T[] a,
 Collection<? extends T> b)
Concatenates specified array with elements from the given collection.
Result is new array with contents of a followed by all other elements (b)
Parameters:
a - array 1
b - collection of elements to be added to the resulting array
Returns:
new array with elements from a followed by elements from b
Since:
7.2

concatenateArrays

@SafeVarargs
public static <T> T[] concatenateArrays(T[] a,
 T... b)
Concatenates specified array with the given elements.
Result is new array with contents of a followed by all other elements (b)
Parameters:
a - array 1
b - array 2 or comma-separated elements
Returns:
new array with elements from a followed by elements from b

concatenateArrays

@SafeVarargs
public static <T> T[] concatenateArrays(T[] a,
 T[]... b)
Concatenates specified array with the given elements.
Result is new array with contents of a followed by all other elements (b)
Parameters:
a - array 1
b - array 2 or comma-separated elements
Returns:
new array with elements from a followed by elements from b
Since:
8.0

toArray

@SafeVarargs
public static <T> T[] toArray(T e,
 T... b)
Creates array using concatenation of the specified element with given array/elements. Result is new array with a followed by all other elements (b)
Parameters:
e - the first element
b - array 2 or comma-separated elements
Returns:
new array with a followed by elements from b