Package org.graph4j.util
Class IntArrays
java.lang.Object
org.graph4j.util.IntArrays
Utility class for working with arrays of integers. Most methods assume that
the values in the arrays are vertex numbers.
- Author:
- Cristian Frăsinaru
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionasList(int[] array) asSet(int[] array) static booleancontains(int[] array, int value) static booleancontains(int[] array, int... values) static booleancontains(int[] array, int value, int fromPos) static booleancontainsDuplicates(int[] array) static int[]copyOf(int[] array) static int[]createMockVertices(int n, int k) static int[]difference(int[] array1, int[] array2) static IntegerfindDuplicate(int[] array) static int[]static int[]static booleanhaveSameValues(int[] array1, int[] array2) static int[]intersection(int[] array1, int[] array2) static booleanintersects(int[] array1, int[] array2) static booleanisSortedAscending(int[] array) Checks if an array is sorted in ascending order.static booleanisSortedDescending(int[] array) Checks if an array is sorted in descending order.static intmax(int[] array) Returns the maximum value in an array.static intmin(int[] array) Returns the minimum value in an array.static int[]positions(int[] array) Creates a new array containing the positions (indices) of the elements in the specified array.static int[]reverse(int[] array) Creates a new array with the elements of the specified array reversed.static int[]shuffle(int[] array) Creates a new array with the original elements shuffled.static int[]Creates a new array with the original elements shuffled.static int[]sort(int[] array) static int[]sort(int[] array, Comparator<Integer> comparator) static int[]sortDesc(int[] array) static StringtoString(int[] array, int fromPos, int toPos) static int[]union(int[]... arrays)
-
Constructor Details
-
IntArrays
public IntArrays()
-
-
Method Details
-
min
public static int min(int[] array) Returns the minimum value in an array.- Parameters:
array- an array of integers.- Returns:
- the minimum value in the array.
-
max
public static int max(int[] array) Returns the maximum value in an array.- Parameters:
array- an array of integers.- Returns:
- the maximum value in the array.
-
copyOf
public static int[] copyOf(int[] array) - Parameters:
array- an array of integers.- Returns:
- a copy of the array.
-
contains
public static boolean contains(int[] array, int value) - Parameters:
array- an array of integers.value- a value.- Returns:
trueif the array contains the value.
-
contains
public static boolean contains(int[] array, int value, int fromPos) - Parameters:
array- an array of integers.value- a value.fromPos- a position in the array.- Returns:
trueif the array contains the value, starting from the given position.
-
contains
public static boolean contains(int[] array, int... values) - Parameters:
array- an array of integers.values- an array of values.- Returns:
trueif the array contains all the values.
-
containsDuplicates
public static boolean containsDuplicates(int[] array) - Parameters:
array- an array of integers.- Returns:
trueif the array contains duplicate values.
-
findDuplicate
- Parameters:
array- an array of integers.- Returns:
- a duplicate value, or
nullif none exists.
-
intersects
public static boolean intersects(int[] array1, int[] array2) - Parameters:
array1- an array of integers.array2- an array of integers.- Returns:
trueif the arrays contain common values.
-
intersection
public static int[] intersection(int[] array1, int[] array2) - Parameters:
array1- an array of integers.array2- an array of integers.- Returns:
- a new array containing the common values.
-
difference
public static int[] difference(int[] array1, int[] array2) - Parameters:
array1- an array of integers.array2- an array of integers.- Returns:
- a new array containing the values in
array1that are not inarray2.
-
asList
- Parameters:
array- an array of integers.- Returns:
- a List representation of the array.
-
asSet
- Parameters:
array- an array of integers.- Returns:
- a Set representation of the array.
-
fromList
- Parameters:
list- a list of integers.- Returns:
- an array representation of the list.
-
fromSet
- Parameters:
set- a set of integers.- Returns:
- an array representation of the set.
-
union
public static int[] union(int[]... arrays) - Parameters:
arrays- an array of array of integers.- Returns:
- the union of all arrays.
-
haveSameValues
public static boolean haveSameValues(int[] array1, int[] array2) - Parameters:
array1- an array of integers.array2- an array of integers.- Returns:
true, if the two arrays have the same values, regardless of order.
-
shuffle
public static int[] shuffle(int[] array) Creates a new array with the original elements shuffled.- Parameters:
array- an array of integers.- Returns:
- a new array with the original elements shuffled.
-
shuffle
Creates a new array with the original elements shuffled.- Parameters:
array- an array of integers.random- a generator of random numbers.- Returns:
- a new array with the original elements shuffled.
-
sort
public static int[] sort(int[] array) - Parameters:
array- an array of integers.- Returns:
- a new array with the original elements sorted ascending.
-
sortDesc
public static int[] sortDesc(int[] array) - Parameters:
array- an array of integers.- Returns:
- a new array with the original elements sorted descending.
-
sort
- Parameters:
array- an array of integers.comparator- a comparator.- Returns:
- a new array with the original elements sorted by the comparator.
-
isSortedAscending
public static boolean isSortedAscending(int[] array) Checks if an array is sorted in ascending order.- Parameters:
array- an array of integers.- Returns:
trueif the array is sorted in ascending order,falseotherwise.
-
isSortedDescending
public static boolean isSortedDescending(int[] array) Checks if an array is sorted in descending order.- Parameters:
array- an array of integers.- Returns:
trueif the array is sorted in descending order,falseotherwise.
-
toString
- Parameters:
array- an array of integers.fromPos- a position in the array.toPos- a position in the array.- Returns:
- a string representation of the specified part of the array.
-
reverse
public static int[] reverse(int[] array) Creates a new array with the elements of the specified array reversed.- Parameters:
array- an array of integers.- Returns:
- the reversed array.
-
positions
public static int[] positions(int[] array) Creates a new array containing the positions (indices) of the elements in the specified array. It assumes that the values in the specified array are all distinct.- Parameters:
array- an array of distinct integers.- Returns:
- the positions of elements in the array.
-
createMockVertices
public static int[] createMockVertices(int n, int k)
-