Class ArrayUtils

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

public class ArrayUtils extends Object
Classe di metodi di utilità per array.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    .
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) static int
    binarySearch(int[] haystack, int needle)
    Finds the index (or insertion point) of an integer in an array of integers in increasing order.
    (package private) static void
    fill(int[] array, int value)
    Fills the given array with the given value.
    (package private) static void
    insertAt(int[] array, int insertionPoint, int value)
    Shift elements of array to the right starting at insertionPoint (inclusive) and inserts value at insertionPoint.
    (package private) static void
    print(int[] array)
    Prints the content of the given array, one element per line.

    Methods inherited from class Object

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

    • ArrayUtils

      private ArrayUtils()
      .
  • Method Details

    • binarySearch

      static int binarySearch(int[] haystack, int needle)
      Finds the index (or insertion point) of an integer in an array of integers in increasing order.

      If the array contains the given integer, returns its index. Otherwise, returns -(insertion_point) - 1 where insertion_point is the index of the first integer greater than needle; note that this implies that the return value is non-negative iff the array contains the integer.

      Parameters:
      haystack - the not null array of integers in increasing order.
      needle - the integer to look for.
      Returns:
      the index of the given integer, or -insertion_point - 1 if none is present.
      Throws:
      NullPointerException - if haystack is null.
      See Also:
    • insertAt

      static void insertAt(int[] array, int insertionPoint, int value)
      Shift elements of array to the right starting at insertionPoint (inclusive) and inserts value at insertionPoint. The last element is discarded to keep the array size constant. Assumes 0 <= insertionPoint < array.length.
      Parameters:
      array - the array where to insert the value.
      insertionPoint - the index where to insert the value.
      value - the value to insert.
      Throws:
      NullPointerException - if array is null.
      ArrayIndexOutOfBoundsException - if insertionPoint is out of bounds.
    • fill

      static void fill(int[] array, int value)
      Fills the given array with the given value.
      Parameters:
      array - the array to fill.
      value - the value to fill the array with.
      Throws:
      NullPointerException - if array is null.
    • print

      static void print(int[] array)
      Prints the content of the given array, one element per line.
      Parameters:
      array - the array to print.
      Throws:
      NullPointerException - if array is null.