Class ArrayUtils


  • public class ArrayUtils
    extends Object
    General methods for arrays
    Author:
    gman6028, markr
    • Constructor Summary

      Constructors 
      Constructor Description
      ArrayUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addTo​(double[] destination, double[] addToDestination)
      Add the values of a second array element-wise to the first array
      static void addTo​(double[] destination, double[] addToDestination, int numberOfElements)
      Add the values of a second array element-wise to the first array
      static Object[] addtoStart​(Object elementToPrepend, Object[] theArray)
      add an element to the start of the passed in array.
      static void divideBy​(double[] destination, double[] diviserArray, double divideByZeroResult)
      Divide the values of the first array by the the second array (element-wise)
      static void divideBy​(double[] destination, double diviser, double divideByZeroResult)
      divide each entry in array by given diviser.
      static void divideBySum​(double[] destination, int divideByZeroResult)
      divide each entry in array by the sum of the entries.
      static double dotProduct​(double[] d1, double[] d2, int numberOfElements)
      Return the dot product of two arrays The dot product is found by multiplying the elements of each array at each position together and then taking the sum of the multiplied values e.g.
      static double getMaximum​(double[] array)
      find the maximum of an array of doubles
      static void loopAll​(double[][] array, BiConsumer<Integer,​Integer> consumer)
      Loop over array entries and apply consumer
      static void loopAll​(double[] array, Consumer<Integer> consumer)
      Loop over array entries and apply consumer
      static <T> void loopAll​(T[] array, Consumer<T> consumer)
      Loop over array entries and apply consumer
      static void multiplyBy​(double[] destination, double multiplicator)
      multiply each entry in array by given multiplicator.
      static double sumOf​(double[] array)
      sum of each entry in array
    • Constructor Detail

      • ArrayUtils

        public ArrayUtils()
    • Method Detail

      • addTo

        public static void addTo​(double[] destination,
                                 double[] addToDestination,
                                 int numberOfElements)
        Add the values of a second array element-wise to the first array
        Parameters:
        destination - the array to be updated
        addToDestination - array of values to be added to destination array
        numberOfElements - number of elements in array to be updated
      • addTo

        public static void addTo​(double[] destination,
                                 double[] addToDestination)
        Add the values of a second array element-wise to the first array
        Parameters:
        destination - the array to be updated
        addToDestination - array of values to be added to destination array
      • divideBy

        public static void divideBy​(double[] destination,
                                    double diviser,
                                    double divideByZeroResult)
        divide each entry in array by given diviser. When divisor is zero, all entries are set to divideByZeroResult
        Parameters:
        destination - array to apply to
        diviser - to divide by
        divideByZeroResult - result if provided division value is zero
      • divideBy

        public static void divideBy​(double[] destination,
                                    double[] diviserArray,
                                    double divideByZeroResult)
        Divide the values of the first array by the the second array (element-wise)
        Parameters:
        destination - the array to be updated
        diviserArray - to divide by these values
        divideByZeroResult - to use in case the diviser is zero
      • multiplyBy

        public static void multiplyBy​(double[] destination,
                                      double multiplicator)
        multiply each entry in array by given multiplicator.
        Parameters:
        destination - array to apply to
        multiplicator - to multiply with
      • divideBySum

        public static void divideBySum​(double[] destination,
                                       int divideByZeroResult)
        divide each entry in array by the sum of the entries. When divisor is zero, all entries are set to divideByZeroResult
        Parameters:
        destination - array to apply to
        divideByZeroResult - result if provided division value is zero
      • sumOf

        public static double sumOf​(double[] array)
        sum of each entry in array
        Parameters:
        array - to apply to
        Returns:
        computed sum
      • dotProduct

        public static double dotProduct​(double[] d1,
                                        double[] d2,
                                        int numberOfElements)
        Return the dot product of two arrays The dot product is found by multiplying the elements of each array at each position together and then taking the sum of the multiplied values e.g. a[1]*b[1]+ a[2]*b[2] + a[3]*b[3] etc
        Parameters:
        d1 - first array in the dot product
        d2 - second array in the dot product
        numberOfElements - number of elements in each array
        Returns:
        the value of the dot product
      • addtoStart

        public static Object[] addtoStart​(Object elementToPrepend,
                                          Object[] theArray)
        add an element to the start of the passed in array. Note that this involves a copy of the original array
        Parameters:
        elementToPrepend - the element to add to start
        theArray - the array to prepend
        Returns:
        the new array
      • getMaximum

        public static double getMaximum​(double[] array)
        find the maximum of an array of doubles
        Parameters:
        array - to check
        Returns:
        maximum value
      • loopAll

        public static void loopAll​(double[][] array,
                                   BiConsumer<Integer,​Integer> consumer)
        Loop over array entries and apply consumer
        Parameters:
        array - to apply consumer to
        consumer - to apply
      • loopAll

        public static void loopAll​(double[] array,
                                   Consumer<Integer> consumer)
        Loop over array entries and apply consumer
        Parameters:
        array - to apply consumer to
        consumer - to apply
      • loopAll

        public static <T> void loopAll​(T[] array,
                                       Consumer<T> consumer)
        Loop over array entries and apply consumer
        Type Parameters:
        T - type of array contents
        Parameters:
        array - to apply consumer to
        consumer - to apply