Package es.urjc.etsii.grafo.util
Class ArrayUtil
java.lang.Object
es.urjc.etsii.grafo.util.ArrayUtil
Util methods to manipulate collections and arrays that are not part of the standard java API
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordstatic final recordstatic final record -
Method Summary
Modifier and TypeMethodDescriptionstatic int[]copyAndshuffle(int[] array) Copy and shuffle an array without modifying the original array.static Object[]copyAndshuffle(Object[] array) Copy and shuffle an array without modifying the original array.static intcountNonNull(Object[] data) Count how many elements are not null in given arraystatic intCount how many elements are null in given arraystatic int[]deleteAndInsert(int[] array, int origin, int destination) Deletes an item from and array and inserts it in the specified position.static long[]deleteAndInsert(long[] array, int origin, int destination) Deletes an item from and array and inserts it in the specified position, moving all elements in between one to the left Example: deleteAndInsert([a,b,c,d,e,f], 0, 1) = [b,a,c,d,e,f] Example: deleteAndInsert([a,b,c,d,e,f], 1, 4) = [a,c,d,e,b,f]static <T> T[]deleteAndInsert(T[] array, int origin, int destination) Deletes an item from and array and inserts it in the specified position.static double[]flatten(double[][] data) Flatten matrix to arraystatic int[]flatten(int[][] data) Flatten matrix to arraystatic long[]flatten(long[][] data) Flatten matrix to arraystatic <T> T[]flatten(T[][] data) Flatten matrix to array.static voidinsert(double[] arr, int index, double value) Insert element in given position.static voidinsert(int[] arr, int index, int value) Insert element in given position.static voidinsert(long[] arr, int index, long value) Insert element in given position.static <T> voidinsert(T[] arr, int index, T value) Insert element in given position.static doublemax(double[] values) Find the biggest value in the given arraystatic intmax(int[] values) Find the biggest value in the given arraystatic longmax(long[] values) Find the biggest value in the given arraystatic String[]Merge arraysstatic doublemin(double[] values) Find the smallest value in the given arraystatic intmin(int[] values) Find the smallest value in the given arraystatic longmin(long[] values) Find the smallest value in the given arraystatic intminIndex(double[] values) Find the position of the minimum value in the array.static intminIndex(int[] values) Find the position of the minimum value in the array.static intminIndex(long[] values) Find the position of the minimum value in the array.static doubleremove(double[] arr, int index) Remove element at given index and shift elements to the left.static intremove(int[] arr, int index) Remove element at given index and shift elements to the left.static longremove(long[] arr, int index) Remove element at given index and shift elements to the left.static <T> Tremove(T[] arr, int index) Remove element at given index and shift elements to the left.static voidreverse(int[] arr) Reverse an arraystatic voidreverse(int[] arr, int start, int end) Reverse a fragment inside an array from start to end (inclusive)static voidReverse an arraystatic voidReverse a fragment inside an array from start to end (inclusive)static voidshuffle(int[] array) Shuffle an array IN PLACE using Fisher–Yates shufflestatic voidShuffle an array IN PLACE using Fisher–Yates shufflestatic ArrayUtil.DoubleStatsstats(double[] data) static ArrayUtil.IntStatsstats(int[] data) static ArrayUtil.LongStatsstats(long[] data) static doublesum(double[] data) Sum all elements in arraystatic intsum(int[] data) Sum all elements in arraystatic longsum(long[] data) Sum all elements in arraystatic voidswap(double[] arr, int i, int j) Swaps the two specified elements in the specified array.static voidswap(int[] arr, int i, int j) Swaps the two specified elements in the specified array.static voidswap(long[] arr, int i, int j) Swaps the two specified elements in the specified array.static voidSwaps the two specified elements in the specified array.
-
Method Details
-
reverse
public static void reverse(int[] arr) Reverse an array- Parameters:
arr- array to reverse
-
reverse
Reverse an array- Parameters:
arr- array to reverse
-
reverse
public static void reverse(int[] arr, int start, int end) Reverse a fragment inside an array from start to end (inclusive)- Parameters:
arr- Array to reversestart- start index, inclusiveend- end index, inclusive
-
reverse
Reverse a fragment inside an array from start to end (inclusive)- Parameters:
arr- Array to reversestart- start index, inclusiveend- end index, inclusive
-
shuffle
public static void shuffle(int[] array) Shuffle an array IN PLACE using Fisher–Yates shuffle- Parameters:
array- Array to shuffle IN PLACE
-
copyAndshuffle
public static int[] copyAndshuffle(int[] array) Copy and shuffle an array without modifying the original array. Uses Fisher–Yates shuffle- Parameters:
array- Array to shuffle- Returns:
- shuffled array. Original array is not modified
-
shuffle
Shuffle an array IN PLACE using Fisher–Yates shuffle- Parameters:
array- Array to shuffle IN PLACE
-
copyAndshuffle
Copy and shuffle an array without modifying the original array. Uses Fisher–Yates shuffle- Parameters:
array- Array to shuffle- Returns:
- shuffled array. Original array is not modified
-
swap
Swaps the two specified elements in the specified array.- Parameters:
arr- arrayi- origin destination indexj- destination index
-
swap
public static void swap(int[] arr, int i, int j) Swaps the two specified elements in the specified array.- Parameters:
arr- arrayi- origin destination indexj- destination index
-
swap
public static void swap(double[] arr, int i, int j) Swaps the two specified elements in the specified array.- Parameters:
arr- arrayi- origin destination indexj- destination index
-
swap
public static void swap(long[] arr, int i, int j) Swaps the two specified elements in the specified array.- Parameters:
arr- arrayi- origin destination indexj- destination index
-
deleteAndInsert
public static <T> T[] deleteAndInsert(T[] array, int origin, int destination) Deletes an item from and array and inserts it in the specified position. Example: deleteAndInsert([a,b,c,d,e,f], 0, 1) = [b,a,c,d,e,f] Example: deleteAndInsert([a,b,c,d,e,f], 1, 4) = [a,c,d,e,b,f] Example: deleteAndInsert([a,b,c,d,e,f], 5, 3) = [a,b,c,f,d,e]- Type Parameters:
T- Array type- Parameters:
array- Array to modifyorigin- index of element to be removeddestination- index where element will be inserted- Returns:
- Modified Array
-
deleteAndInsert
public static int[] deleteAndInsert(int[] array, int origin, int destination) Deletes an item from and array and inserts it in the specified position. Example: deleteAndInsert([a,b,c,d,e,f], 0, 1) = [b,a,c,d,e,f] Example: deleteAndInsert([a,b,c,d,e,f], 1, 4) = [a,c,d,e,b,f]- Parameters:
array- Array to modifyorigin- index of element to be removeddestination- index where element will be inserted- Returns:
- Modified Array
-
deleteAndInsert
public static long[] deleteAndInsert(long[] array, int origin, int destination) Deletes an item from and array and inserts it in the specified position, moving all elements in between one to the left Example: deleteAndInsert([a,b,c,d,e,f], 0, 1) = [b,a,c,d,e,f] Example: deleteAndInsert([a,b,c,d,e,f], 1, 4) = [a,c,d,e,b,f]- Parameters:
array- Array to modifyorigin- index of element to be removeddestination- index where element will be inserted- Returns:
- Modified Array
-
insert
public static void insert(int[] arr, int index, int value) Insert element in given position. Elements to the right are shifted one position to the right. Rightmost element is dropped.- Parameters:
arr- Array to modifyindex- Position in which insert the elementvalue- Element to insert
-
insert
public static void insert(long[] arr, int index, long value) Insert element in given position. Elements to the right are shifted one position to the right. Rightmost element is dropped.- Parameters:
arr- Array to modifyindex- Position in which insert the elementvalue- Element to insert
-
insert
public static void insert(double[] arr, int index, double value) Insert element in given position. Elements to the right are shifted one position to the right. Rightmost element is dropped.- Parameters:
arr- Array to modifyindex- Position in which insert the elementvalue- Element to insert
-
insert
public static <T> void insert(T[] arr, int index, T value) Insert element in given position. Elements to the right are shifted one position to the right. Rightmost element is dropped.- Type Parameters:
T- type- Parameters:
arr- Array to modifyindex- Position in which insert the elementvalue- Element to insert
-
remove
public static int remove(int[] arr, int index) Remove element at given index and shift elements to the left. Rightmost element is duplicated. Example: remove([9,10,11,12], 1) → [9,11,12,12]- Parameters:
arr- array to modifyindex- index of element to delete- Returns:
- removed element
-
remove
public static long remove(long[] arr, int index) Remove element at given index and shift elements to the left. Rightmost element is duplicated. Example: remove([9,10,11,12], 1) → [9,11,12,12]- Parameters:
arr- array to modifyindex- index of element to delete- Returns:
- removed element
-
remove
public static double remove(double[] arr, int index) Remove element at given index and shift elements to the left. Rightmost element is duplicated. Example: remove([9,10,11,12], 1) → [9,11,12,12]- Parameters:
arr- array to modifyindex- index of element to delete- Returns:
- removed element
-
remove
public static <T> T remove(T[] arr, int index) Remove element at given index and shift elements to the left. Rightmost element is duplicated. Example: remove([9,10,11,12], 1) → [9,11,12,12]- Type Parameters:
T- type- Parameters:
arr- array to modifyindex- index of element to delete- Returns:
- removed element
-
flatten
public static int[] flatten(int[][] data) Flatten matrix to array- Parameters:
data- array data- Returns:
- flattened array
-
flatten
public static double[] flatten(double[][] data) Flatten matrix to array- Parameters:
data- array data- Returns:
- flattened array
-
flatten
public static long[] flatten(long[][] data) Flatten matrix to array- Parameters:
data- array data- Returns:
- flattened array
-
flatten
public static <T> T[] flatten(T[][] data) Flatten matrix to array. Matrix must be initialized, even if each row has a different size. Having null arrays is not valid, having null values is perfectly valid- Parameters:
data- array data- Returns:
- flattened array
-
countNonNull
Count how many elements are not null in given array- Parameters:
data- array- Returns:
- number of non null elements
-
countNull
Count how many elements are null in given array- Parameters:
data- array- Returns:
- number of null elements
-
sum
public static int sum(int[] data) Sum all elements in array- Parameters:
data- numbers to sum- Returns:
- sum of all numbers
- Throws:
ArithmeticException- if there is an overflow
-
sum
public static double sum(double[] data) Sum all elements in array- Parameters:
data- numbers to sum- Returns:
- sum of all numbers
-
sum
public static long sum(long[] data) Sum all elements in array- Parameters:
data- numbers to sum- Returns:
- sum of all numbers
- Throws:
ArithmeticException- if there is an overflow
-
max
public static double max(double[] values) Find the biggest value in the given array- Parameters:
values- array of double numbers. Both positive and negative infinity are valid values.- Returns:
- biggest value in array
- Throws:
IllegalArgumentException- if the array contains a NaN
-
max
public static int max(int[] values) Find the biggest value in the given array- Parameters:
values- array of integer numbers.- Returns:
- biggest value in array
-
max
public static long max(long[] values) Find the biggest value in the given array- Parameters:
values- array of long numbers.- Returns:
- biggest value in array
-
min
public static double min(double[] values) Find the smallest value in the given array- Parameters:
values- array of double numbers. Both positive and negative infinity are valid values.- Returns:
- smallest value in array
- Throws:
IllegalArgumentException- if the array contains a NaN
-
min
public static int min(int[] values) Find the smallest value in the given array- Parameters:
values- array of integer numbers.- Returns:
- smallest value in array
-
min
public static long min(long[] values) Find the smallest value in the given array- Parameters:
values- array of long numbers.- Returns:
- smallest value in array
-
minIndex
public static int minIndex(int[] values) Find the position of the minimum value in the array. If multiple positions have the same minimum value, returns the lower index.- Parameters:
values- array of values- Returns:
- position of the minimum value in the array
-
minIndex
public static int minIndex(long[] values) Find the position of the minimum value in the array. If multiple positions have the same minimum value, returns the lower index.- Parameters:
values- array of values- Returns:
- position of the minimum value in the array
-
minIndex
public static int minIndex(double[] values) Find the position of the minimum value in the array. If multiple positions have the same minimum value, returns the lower index.- Parameters:
values- array of values- Returns:
- position of the minimum value in the array
-
merge
Merge arrays- Parameters:
arrs- arrays to merge- Returns:
- array with all elements in same order
-
stats
-
stats
-
stats
-