Class IntArrays

java.lang.Object
org.graph4j.util.IntArrays

public class IntArrays extends Object
Utility class for working with arrays of integers. Most methods assume that the values in the arrays are vertex numbers.
Author:
Cristian Frăsinaru
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static List<Integer>
    asList(int[] array)
     
    static Set<Integer>
    asSet(int[] array)
     
    static boolean
    contains(int[] array, int value)
     
    static boolean
    contains(int[] array, int... values)
     
    static boolean
    contains(int[] array, int value, int fromPos)
     
    static boolean
    containsDuplicates(int[] array)
     
    static int[]
    copyOf(int[] array)
     
    static int[]
    createMockVertices(int n, int k)
     
    static int[]
    difference(int[] array1, int[] array2)
     
    static Integer
    findDuplicate(int[] array)
     
    static int[]
     
    static int[]
     
    static boolean
    haveSameValues(int[] array1, int[] array2)
     
    static int[]
    intersection(int[] array1, int[] array2)
     
    static boolean
    intersects(int[] array1, int[] array2)
     
    static boolean
    isSortedAscending(int[] array)
    Checks if an array is sorted in ascending order.
    static boolean
    isSortedDescending(int[] array)
    Checks if an array is sorted in descending order.
    static int
    max(int[] array)
    Returns the maximum value in an array.
    static int
    min(int[] array)
    Returns the minimum value in an array.
    static int[]
    positions(int[] array)
    Creates a new array containing the positions (indices) of the elements in the specified array.
    static int[]
    reverse(int[] array)
    Creates a new array with the elements of the specified array reversed.
    static int[]
    shuffle(int[] array)
    Creates a new array with the original elements shuffled.
    static int[]
    shuffle(int[] array, Random random)
    Creates a new array with the original elements shuffled.
    static int[]
    sort(int[] array)
     
    static int[]
    sort(int[] array, Comparator<Integer> comparator)
     
    static int[]
    sortDesc(int[] array)
     
    static String
    toString(int[] array, int fromPos, int toPos)
     
    static int[]
    union(int[]... arrays)
     

    Methods inherited from class java.lang.Object

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

    • IntArrays

      public IntArrays()
  • Method Details

    • min

      public static int min(int[] array)
      Returns the minimum value in an array.
      Parameters:
      array - an array of integers.
      Returns:
      the minimum value in the array.
    • max

      public static int max(int[] array)
      Returns the maximum value in an array.
      Parameters:
      array - an array of integers.
      Returns:
      the maximum value in the array.
    • copyOf

      public static int[] copyOf(int[] array)
      Parameters:
      array - an array of integers.
      Returns:
      a copy of the array.
    • contains

      public static boolean contains(int[] array, int value)
      Parameters:
      array - an array of integers.
      value - a value.
      Returns:
      true if the array contains the value.
    • contains

      public static boolean contains(int[] array, int value, int fromPos)
      Parameters:
      array - an array of integers.
      value - a value.
      fromPos - a position in the array.
      Returns:
      true if the array contains the value, starting from the given position.
    • contains

      public static boolean contains(int[] array, int... values)
      Parameters:
      array - an array of integers.
      values - an array of values.
      Returns:
      true if the array contains all the values.
    • containsDuplicates

      public static boolean containsDuplicates(int[] array)
      Parameters:
      array - an array of integers.
      Returns:
      true if the array contains duplicate values.
    • findDuplicate

      public static Integer findDuplicate(int[] array)
      Parameters:
      array - an array of integers.
      Returns:
      a duplicate value, or null if none exists.
    • intersects

      public static boolean intersects(int[] array1, int[] array2)
      Parameters:
      array1 - an array of integers.
      array2 - an array of integers.
      Returns:
      true if the arrays contain common values.
    • intersection

      public static int[] intersection(int[] array1, int[] array2)
      Parameters:
      array1 - an array of integers.
      array2 - an array of integers.
      Returns:
      a new array containing the common values.
    • difference

      public static int[] difference(int[] array1, int[] array2)
      Parameters:
      array1 - an array of integers.
      array2 - an array of integers.
      Returns:
      a new array containing the values in array1 that are not in array2.
    • asList

      public static List<Integer> asList(int[] array)
      Parameters:
      array - an array of integers.
      Returns:
      a List representation of the array.
    • asSet

      public static Set<Integer> asSet(int[] array)
      Parameters:
      array - an array of integers.
      Returns:
      a Set representation of the array.
    • fromList

      public static int[] fromList(List<Integer> list)
      Parameters:
      list - a list of integers.
      Returns:
      an array representation of the list.
    • fromSet

      public static int[] fromSet(Set<Integer> set)
      Parameters:
      set - a set of integers.
      Returns:
      an array representation of the set.
    • union

      public static int[] union(int[]... arrays)
      Parameters:
      arrays - an array of array of integers.
      Returns:
      the union of all arrays.
    • haveSameValues

      public static boolean haveSameValues(int[] array1, int[] array2)
      Parameters:
      array1 - an array of integers.
      array2 - an array of integers.
      Returns:
      true, if the two arrays have the same values, regardless of order.
    • shuffle

      public static int[] shuffle(int[] array)
      Creates a new array with the original elements shuffled.
      Parameters:
      array - an array of integers.
      Returns:
      a new array with the original elements shuffled.
    • shuffle

      public static int[] shuffle(int[] array, Random random)
      Creates a new array with the original elements shuffled.
      Parameters:
      array - an array of integers.
      random - a generator of random numbers.
      Returns:
      a new array with the original elements shuffled.
    • sort

      public static int[] sort(int[] array)
      Parameters:
      array - an array of integers.
      Returns:
      a new array with the original elements sorted ascending.
    • sortDesc

      public static int[] sortDesc(int[] array)
      Parameters:
      array - an array of integers.
      Returns:
      a new array with the original elements sorted descending.
    • sort

      public static int[] sort(int[] array, Comparator<Integer> comparator)
      Parameters:
      array - an array of integers.
      comparator - a comparator.
      Returns:
      a new array with the original elements sorted by the comparator.
    • isSortedAscending

      public static boolean isSortedAscending(int[] array)
      Checks if an array is sorted in ascending order.
      Parameters:
      array - an array of integers.
      Returns:
      true if the array is sorted in ascending order, false otherwise.
    • isSortedDescending

      public static boolean isSortedDescending(int[] array)
      Checks if an array is sorted in descending order.
      Parameters:
      array - an array of integers.
      Returns:
      true if the array is sorted in descending order, false otherwise.
    • toString

      public static String toString(int[] array, int fromPos, int toPos)
      Parameters:
      array - an array of integers.
      fromPos - a position in the array.
      toPos - a position in the array.
      Returns:
      a string representation of the specified part of the array.
    • reverse

      public static int[] reverse(int[] array)
      Creates a new array with the elements of the specified array reversed.
      Parameters:
      array - an array of integers.
      Returns:
      the reversed array.
    • positions

      public static int[] positions(int[] array)
      Creates a new array containing the positions (indices) of the elements in the specified array. It assumes that the values in the specified array are all distinct.
      Parameters:
      array - an array of distinct integers.
      Returns:
      the positions of elements in the array.
    • createMockVertices

      public static int[] createMockVertices(int n, int k)