Class ArrayUtils

java.lang.Object
it.unimi.di.prog2.h22.ArrayUtils

public class ArrayUtils extends Object
A class providing some utility methods for arrays.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    .
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> int
    countNull(T[] vals)
    Counts the number of null elements in an array.
    static <T extends Comparable<T>>
    int
    countSmaller(T[] array, T element)
    Counts the number of elements in an array that are smaller than a given element.
    static <T> int
    linearSearch(T[] haystack, T needle)
    Searches for a needle in a haystack.
    static <U, T extends U>
    void
    pour(List<T> src, List<U> dst)
    Pours the elements of a list into another list.

    Methods inherited from class java.lang.Object

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

    • ArrayUtils

      private ArrayUtils()
      .
  • Method Details

    • countNull

      public static <T> int countNull(T[] vals)
      Counts the number of null elements in an array.
      Type Parameters:
      T - the type of the elements in the haystack.
      Parameters:
      vals - the array.
      Returns:
      the number of null elements in vals.
      Throws:
      NullPointerException - if vals is null.
    • linearSearch

      public static <T> int linearSearch(T[] haystack, T needle)
      Searches for a needle in a haystack.

      Returns an index i such that haystack[i] == needle, or -1 if the needle does not appear in the haystack.

      Type Parameters:
      T - the type of the elements in the haystack.
      Parameters:
      haystack - the haystack.
      needle - the needle.
      Returns:
      an index i such that haystack[i] == needle, or -1 if the needle does not appear in the haystack.
      Throws:
      NullPointerException - if haystack or needle are null.
    • countSmaller

      public static <T extends Comparable<T>> int countSmaller(T[] array, T element)
      Counts the number of elements in an array that are smaller than a given element.
      Type Parameters:
      T - the type of the elements in the array.
      Parameters:
      array - the array.
      element - the element to compare.
      Returns:
      the number of elements in array that are smaller than element.
      Throws:
      NullPointerException - if array is null.
    • pour

      public static <U, T extends U> void pour(List<T> src, List<U> dst)
      Pours the elements of a list into another list.
      Type Parameters:
      U - the type of the elements of the destination list.
      T - the type of the elements of the source list.
      Parameters:
      src - the source list.
      dst - the destination list.
      Throws:
      NullPointerException - if src or dst are null.