Class ArraysUtil

java.lang.Object
space.arim.omnibus.util.ArraysUtil

public final class ArraysUtil extends Object
Utility class for manipulating arrays, particularly sorted arrays, reducing the need to write fast, error-prone array operations.
Author:
A248
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T[]
    contractAndRemove(T[] sourceArray, int removalIndex)
    Copies all the elements in the target array, except the element at removalIndex, to a new array, and returns the resulting array.
    static <T> T[]
    expandAndInsert(T[] sourceArray, T element, int insertionIndex)
    Creates a new array of the source array's length plus 1, then copies all the elements before the insertion index from the source array to the new array, and copies all elements after the insertion index to the new array.

    Methods inherited from class Object

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

    • expandAndInsert

      public static <T> T[] expandAndInsert(T[] sourceArray, T element, int insertionIndex)
      Creates a new array of the source array's length plus 1, then copies all the elements before the insertion index from the source array to the new array, and copies all elements after the insertion index to the new array. Then the element at the insertion index is set to the specified element.
      Type Parameters:
      T - the array type
      Parameters:
      sourceArray - the source array
      element - the element to insert
      insertionIndex - the index at which to insert the element
      Returns:
      the expanded array
    • contractAndRemove

      public static <T> T[] contractAndRemove(T[] sourceArray, int removalIndex)
      Copies all the elements in the target array, except the element at removalIndex, to a new array, and returns the resulting array. In this way all the elements before the removalIndex are unaffected, and all those after the index are shifted up one, such that the order of existing elements is maintained.
      Type Parameters:
      T - the array type
      Parameters:
      sourceArray - the source array
      removalIndex - the index to skip
      Returns:
      the contracted array