Class ArrayUtil

java.lang.Object
es.urjc.etsii.grafo.util.ArrayUtil

public class ArrayUtil extends Object
Util methods to manipulate collections and arrays that are not part of the standard java API
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
     
    static final record 
     
    static final record 
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int[]
    copyAndshuffle(int[] array)
    Copy and shuffle an array without modifying the original array.
    static Object[]
    Copy and shuffle an array without modifying the original array.
    static int
    Count how many elements are not null in given array
    static int
    countNull(Object[] data)
    Count how many elements are null in given array
    static 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 array
    static int[]
    flatten(int[][] data)
    Flatten matrix to array
    static long[]
    flatten(long[][] data)
    Flatten matrix to array
    static <T> T[]
    flatten(T[][] data)
    Flatten matrix to array.
    static void
    insert(double[] arr, int index, double value)
    Insert element in given position.
    static void
    insert(int[] arr, int index, int value)
    Insert element in given position.
    static void
    insert(long[] arr, int index, long value)
    Insert element in given position.
    static <T> void
    insert(T[] arr, int index, T value)
    Insert element in given position.
    static double
    max(double[] values)
    Find the biggest value in the given array
    static int
    max(int[] values)
    Find the biggest value in the given array
    static long
    max(long[] values)
    Find the biggest value in the given array
    static String[]
    merge(String[]... arrs)
    Merge arrays
    static double
    min(double[] values)
    Find the smallest value in the given array
    static int
    min(int[] values)
    Find the smallest value in the given array
    static long
    min(long[] values)
    Find the smallest value in the given array
    static int
    minIndex(double[] values)
    Find the position of the minimum value in the array.
    static int
    minIndex(int[] values)
    Find the position of the minimum value in the array.
    static int
    minIndex(long[] values)
    Find the position of the minimum value in the array.
    static double
    remove(double[] arr, int index)
    Remove element at given index and shift elements to the left.
    static int
    remove(int[] arr, int index)
    Remove element at given index and shift elements to the left.
    static long
    remove(long[] arr, int index)
    Remove element at given index and shift elements to the left.
    static <T> T
    remove(T[] arr, int index)
    Remove element at given index and shift elements to the left.
    static void
    reverse(int[] arr)
    Reverse an array
    static void
    reverse(int[] arr, int start, int end)
    Reverse a fragment inside an array from start to end (inclusive)
    static void
    reverse(Object[] arr)
    Reverse an array
    static void
    reverse(Object[] arr, int start, int end)
    Reverse a fragment inside an array from start to end (inclusive)
    static void
    shuffle(int[] array)
    Shuffle an array IN PLACE using Fisher–Yates shuffle
    static void
    shuffle(Object[] array)
    Shuffle an array IN PLACE using Fisher–Yates shuffle
    stats(double[] data)
     
    stats(int[] data)
     
    stats(long[] data)
     
    static double
    sum(double[] data)
    Sum all elements in array
    static int
    sum(int[] data)
    Sum all elements in array
    static long
    sum(long[] data)
    Sum all elements in array
    static void
    swap(double[] arr, int i, int j)
    Swaps the two specified elements in the specified array.
    static void
    swap(int[] arr, int i, int j)
    Swaps the two specified elements in the specified array.
    static void
    swap(long[] arr, int i, int j)
    Swaps the two specified elements in the specified array.
    static void
    swap(Object[] arr, int i, int j)
    Swaps the two specified elements in the specified array.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • reverse

      public static void reverse(int[] arr)
      Reverse an array
      Parameters:
      arr - array to reverse
    • reverse

      public static void reverse(Object[] arr)
      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 reverse
      start - start index, inclusive
      end - end index, inclusive
    • reverse

      public static void reverse(Object[] arr, int start, int end)
      Reverse a fragment inside an array from start to end (inclusive)
      Parameters:
      arr - Array to reverse
      start - start index, inclusive
      end - 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

      public static void shuffle(Object[] array)
      Shuffle an array IN PLACE using Fisher–Yates shuffle
      Parameters:
      array - Array to shuffle IN PLACE
    • copyAndshuffle

      public static Object[] copyAndshuffle(Object[] 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
    • swap

      public static void swap(Object[] arr, int i, int j)
      Swaps the two specified elements in the specified array.
      Parameters:
      arr - array
      i - origin destination index
      j - destination index
    • swap

      public static void swap(int[] arr, int i, int j)
      Swaps the two specified elements in the specified array.
      Parameters:
      arr - array
      i - origin destination index
      j - destination index
    • swap

      public static void swap(double[] arr, int i, int j)
      Swaps the two specified elements in the specified array.
      Parameters:
      arr - array
      i - origin destination index
      j - destination index
    • swap

      public static void swap(long[] arr, int i, int j)
      Swaps the two specified elements in the specified array.
      Parameters:
      arr - array
      i - origin destination index
      j - 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 modify
      origin - index of element to be removed
      destination - 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 modify
      origin - index of element to be removed
      destination - 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 modify
      origin - index of element to be removed
      destination - 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 modify
      index - Position in which insert the element
      value - 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 modify
      index - Position in which insert the element
      value - 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 modify
      index - Position in which insert the element
      value - 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 modify
      index - Position in which insert the element
      value - 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 modify
      index - 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 modify
      index - 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 modify
      index - 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 modify
      index - 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

      public static int countNonNull(Object[] data)
      Count how many elements are not null in given array
      Parameters:
      data - array
      Returns:
      number of non null elements
    • countNull

      public static int countNull(Object[] data)
      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

      public static String[] merge(String[]... arrs)
      Merge arrays
      Parameters:
      arrs - arrays to merge
      Returns:
      array with all elements in same order
    • stats

      public static ArrayUtil.IntStats stats(int[] data)
    • stats

      public static ArrayUtil.LongStats stats(long[] data)
    • stats

      public static ArrayUtil.DoubleStats stats(double[] data)