Class ArraysUtil
java.lang.Object
space.arim.omnibus.util.ArraysUtil
Utility class for manipulating arrays, particularly sorted arrays, reducing the need to write
fast, error-prone array operations.
- Author:
- A248
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T[]contractAndRemove(T[] sourceArray, int removalIndex) Copies all the elements in the target array, except the element atremovalIndex, 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.
-
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 arrayelement- the element to insertinsertionIndex- 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 atremovalIndex, 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 arrayremovalIndex- the index to skip- Returns:
- the contracted array
-