Class OpenArray
java.lang.Object
cloud.opencode.base.core.OpenArray
Array Utility Class - Comprehensive array operations for primitive and object arrays
数组工具类 - 支持原始类型和对象数组的全面操作
Provides array creation, manipulation, search, conversion and collection operations.
提供数组创建、操作、搜索、转换和集合操作功能。参考 Apache Commons ArrayUtils。
Features | 主要功能:
- Array creation (of, newArray, nullToEmpty) - 数组创建
- Add/Insert/Remove operations - 添加/插入/删除操作
- Search (contains, indexOf, lastIndexOf) - 搜索
- Operations (reverse, shuffle, rotate, swap) - 操作
- Primitive/Wrapper conversion (toPrimitive, toObject) - 原始/包装类型转换
- Collection conversion (toList, toSet, toMap) - 集合转换
Usage Examples | 使用示例:
// Create array - 创建数组
String[] arr = OpenArray.of("a", "b", "c");
// Add element - 添加元素
String[] newArr = OpenArray.add(arr, "d");
// Search - 搜索
boolean contains = OpenArray.contains(arr, "b");
int index = OpenArray.indexOf(arr, "b");
// Convert - 转换
List<String> list = OpenArray.toList(arr);
int[] primitives = OpenArray.toPrimitive(integers);
Security | 安全性:
- Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-core V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T[]add(T[] array, int index, T element) Inserts an element at the specified index.static <T> T[]add(T[] array, T element) Appends an element to the end of the array.static <T> T[]addAll(T[] array1, T... array2) Concatenates two arrays into a new array.static booleancontains(int[] array, int element) Returns true if an int array contains the specified element.static <T> booleancontains(T[] array, T element) Returns true if the array contains the specified element.static <T> T[]Filters the array, returning a new array containing only elements matching the predicate.static <T> Optional<T> getFirst(T[] array) Returns the first element of the array wrapped in an Optional.static <T> Optional<T> getLast(T[] array) Returns the last element of the array wrapped in an Optional.static <T> intindexOf(T[] array, T element) Returns the index of the first occurrence of the element in the array.static <T> intindexOf(T[] array, T element, int startIndex) Returns the index of the first occurrence of the element starting from startIndex.static int[]insert(int index, int[] array, int... values) Inserts multiple int elements at the specified index.static <T> T[]insert(int index, T[] array, T... values) Inserts multiple elements at the specified index.static booleanisEmpty(int[] array) Returns true if an int array is null or has length zero.static booleanReturns true if the array is null or has length zero.static booleanisNotEmpty(Object[] array) Returns true if the array is not null and has at least one element.static booleanisSameLength(Object[] array1, Object[] array2) Returns true if both arrays have the same length.static booleanisSorted(int[] array) Returns true if the int array is sorted in ascending order.static <T extends Comparable<T>>
booleanisSorted(T[] array) Returns true if the Comparable array is sorted in ascending order.static <T> intlastIndexOf(T[] array, T element) Returns the index of the last occurrence of the element in the array.static <T,R> R[] Maps each element of the array using the given mapper function.static <T> T[]Creates an array of the specified component type and length.static int[]nullToEmpty(int[] array) Converts a null int array to an empty int array.static long[]nullToEmpty(long[] array) Converts a null long array to an empty long array.static <T> T[]nullToEmpty(T[] array, Class<T[]> type) Converts a null array to an empty array of the specified type.static <T> T[]of(T... elements) Creates an array containing the specified elements.static <T> T[]remove(T[] array, int index) Removes the element at the specified index.static int[]removeAll(int[] array, int... indices) Removes elements at multiple indices from an int array.static <T> T[]removeAll(T[] array, int... indices) Removes elements at multiple indices.static <T> T[]removeElement(T[] array, T element) Removes the first occurrence of the specified element.static voidreverse(int[] array) Reverses the order of the elements in an int array.static <T> voidreverse(T[] array) Reverses the order of the elements in the array.static voidRotates the elements of the array by the given distance (positive = right, negative = left).static <T> voidshuffle(T[] array) Randomly shuffles the elements of the array.static byte[]subarray(byte[] array, int startInclusive, int endExclusive) Returns a byte subarray from startInclusive to endExclusive.static int[]subarray(int[] array, int startInclusive, int endExclusive) Returns an int subarray from startInclusive to endExclusive.static long[]subarray(long[] array, int startInclusive, int endExclusive) Returns a long subarray from startInclusive to endExclusive.static <T> T[]subarray(T[] array, int startInclusive, int endExclusive) Returns a subarray from startInclusive (inclusive) to endExclusive (exclusive).static voidswap(int[] array, int offset1, int offset2) Swaps two elements in an int array.static voidSwaps two elements in the array.static voidSwaps a series of elements of the given length starting at two offsets.static <T> List<T> toList(T... array) Converts an array to a mutable List.static <K,V> Map <K, V> Converts a two-dimensional array to a Map.static Boolean[]toObject(boolean[] array) Converts a boolean array to a Boolean array. boolean 数组转 Boolean 数组static Byte[]toObject(byte[] array) Converts a byte array to a Byte array. byte 数组转 Byte 数组static Character[]toObject(char[] array) Converts a char array to a Character array. char 数组转 Character 数组static Double[]toObject(double[] array) Converts a double array to a Double array. double 数组转 Double 数组static Integer[]toObject(int[] array) Converts an int array to an Integer array. int 数组转 Integer 数组static Long[]toObject(long[] array) Converts a long array to a Long array. long 数组转 Long 数组static boolean[]toPrimitive(Boolean[] array) Converts a Boolean array to a boolean array.static byte[]toPrimitive(Byte[] array) Converts a Byte array to a byte array.static char[]toPrimitive(Character[] array) Converts a Character array to a char array.static double[]toPrimitive(Double[] array) Converts a Double array to a double array.static int[]toPrimitive(Integer[] array) Converts an Integer array to an int array.static int[]toPrimitive(Integer[] array, int valueForNull) Converts an Integer array to an int array with a specified value for null elements.static long[]toPrimitive(Long[] array) Converts a Long array to a long array.static <T> Set<T> toSet(T... array) Converts an array to a Set.
-
Field Details
-
EMPTY_INT_ARRAY
public static final int[] EMPTY_INT_ARRAY -
EMPTY_LONG_ARRAY
public static final long[] EMPTY_LONG_ARRAY -
EMPTY_DOUBLE_ARRAY
public static final double[] EMPTY_DOUBLE_ARRAY -
EMPTY_FLOAT_ARRAY
public static final float[] EMPTY_FLOAT_ARRAY -
EMPTY_SHORT_ARRAY
public static final short[] EMPTY_SHORT_ARRAY -
EMPTY_BYTE_ARRAY
public static final byte[] EMPTY_BYTE_ARRAY -
EMPTY_CHAR_ARRAY
public static final char[] EMPTY_CHAR_ARRAY -
EMPTY_BOOLEAN_ARRAY
public static final boolean[] EMPTY_BOOLEAN_ARRAY -
EMPTY_STRING_ARRAY
-
EMPTY_OBJECT_ARRAY
-
-
Method Details
-
newArray
Creates an array of the specified component type and length. 创建指定类型和长度的数组- Type Parameters:
T- element type | 元素类型- Parameters:
componentType- component type of the array | 数组元素类型length- length of the array | 数组长度- Returns:
- the newly created array | 新创建的数组
-
of
Creates an array containing the specified elements. 创建包含指定元素的数组- Type Parameters:
T- element type | 元素类型- Parameters:
elements- elements to include | 元素- Returns:
- the array | 数组
-
nullToEmpty
Converts a null array to an empty array of the specified type. 将 null 数组转换为空数组- Type Parameters:
T- element type | 元素类型- Parameters:
array- array, may be null | 数组type- array type | 数组类型- Returns:
- the original array, or an empty array if null | 非 null 数组
-
nullToEmpty
public static int[] nullToEmpty(int[] array) Converts a null int array to an empty int array. 将 null int 数组转换为空数组- Parameters:
array- array, may be null | 数组- Returns:
- the original array, or an empty array if null | 非 null 数组
-
nullToEmpty
public static long[] nullToEmpty(long[] array) Converts a null long array to an empty long array. 将 null long 数组转换为空数组- Parameters:
array- array, may be null | 数组- Returns:
- the original array, or an empty array if null | 非 null 数组
-
add
public static <T> T[] add(T[] array, T element) Appends an element to the end of the array. 在数组末尾添加元素- Type Parameters:
T- element type | 元素类型- Parameters:
array- original array | 原数组element- element to add | 要添加的元素- Returns:
- new array with the element appended | 新数组
-
add
public static <T> T[] add(T[] array, int index, T element) Inserts an element at the specified index. 在指定位置插入元素- Type Parameters:
T- element type | 元素类型- Parameters:
array- original array | 原数组index- insertion index | 插入位置element- element to insert | 要插入的元素- Returns:
- new array with the element inserted | 新数组
-
addAll
Concatenates two arrays into a new array. 合并两个数组- Type Parameters:
T- element type | 元素类型- Parameters:
array1- first array | 数组1array2- second array | 数组2- Returns:
- merged array | 合并后的数组
-
insert
Inserts multiple elements at the specified index. 在指定位置插入多个元素- Type Parameters:
T- element type | 元素类型- Parameters:
index- insertion index | 插入位置array- original array | 原数组values- elements to insert | 要插入的元素- Returns:
- new array with elements inserted | 新数组
-
insert
public static int[] insert(int index, int[] array, int... values) Inserts multiple int elements at the specified index. 在指定位置插入多个 int 元素- Parameters:
index- insertion index | 插入位置array- original array | 原数组values- elements to insert | 要插入的元素- Returns:
- new array with elements inserted | 新数组
-
remove
public static <T> T[] remove(T[] array, int index) Removes the element at the specified index. 删除指定索引的元素- Type Parameters:
T- element type | 元素类型- Parameters:
array- original array | 原数组index- index to remove | 要删除的索引- Returns:
- new array with the element removed | 新数组
-
removeElement
public static <T> T[] removeElement(T[] array, T element) Removes the first occurrence of the specified element. 删除指定元素(首次出现)- Type Parameters:
T- element type | 元素类型- Parameters:
array- original array | 原数组element- element to remove | 要删除的元素- Returns:
- new array with the element removed | 新数组
-
removeAll
public static <T> T[] removeAll(T[] array, int... indices) Removes elements at multiple indices. 删除多个索引位置的元素- Type Parameters:
T- element type | 元素类型- Parameters:
array- original array | 原数组indices- indices to remove | 要删除的索引- Returns:
- new array with elements removed | 新数组
-
removeAll
public static int[] removeAll(int[] array, int... indices) Removes elements at multiple indices from an int array. 删除多个 int 数组索引位置的元素- Parameters:
array- original array | 原数组indices- indices to remove | 要删除的索引- Returns:
- new array with elements removed | 新数组
-
subarray
public static <T> T[] subarray(T[] array, int startInclusive, int endExclusive) Returns a subarray from startInclusive (inclusive) to endExclusive (exclusive). 获取子数组- Type Parameters:
T- element type | 元素类型- Parameters:
array- original array | 原数组startInclusive- start index, inclusive | 起始索引(包含)endExclusive- end index, exclusive | 结束索引(不包含)- Returns:
- the subarray | 子数组
-
subarray
public static int[] subarray(int[] array, int startInclusive, int endExclusive) Returns an int subarray from startInclusive to endExclusive. 获取 int 子数组 -
subarray
public static long[] subarray(long[] array, int startInclusive, int endExclusive) Returns a long subarray from startInclusive to endExclusive. 获取 long 子数组 -
subarray
public static byte[] subarray(byte[] array, int startInclusive, int endExclusive) Returns a byte subarray from startInclusive to endExclusive. 获取 byte 子数组 -
swap
Swaps two elements in the array. 交换数组中两个位置的元素- Parameters:
array- the array | 数组offset1- first position | 位置1offset2- second position | 位置2
-
swap
public static void swap(int[] array, int offset1, int offset2) Swaps two elements in an int array. 交换 int 数组中两个位置的元素 -
swap
Swaps a series of elements of the given length starting at two offsets. 交换数组中指定长度的元素- Parameters:
array- the array | 数组offset1- first offset | 位置1offset2- second offset | 位置2len- number of elements to swap | 交换长度
-
reverse
public static <T> void reverse(T[] array) Reverses the order of the elements in the array. 反转数组- Type Parameters:
T- element type | 元素类型- Parameters:
array- the array to reverse | 数组
-
reverse
public static void reverse(int[] array) Reverses the order of the elements in an int array. 反转 int 数组 -
shuffle
public static <T> void shuffle(T[] array) Randomly shuffles the elements of the array. 打乱数组- Type Parameters:
T- element type | 元素类型- Parameters:
array- the array to shuffle | 数组
-
rotate
Rotates the elements of the array by the given distance (positive = right, negative = left). 旋转数组- Parameters:
array- the array to rotate | 数组distance- rotation distance (positive = right, negative = left) | 旋转距离(正数向右,负数向左)
-
contains
public static <T> boolean contains(T[] array, T element) Returns true if the array contains the specified element. 检查数组是否包含指定元素- Type Parameters:
T- element type | 元素类型- Parameters:
array- the array | 数组element- element to search for | 元素- Returns:
- true if found | 如果包含返回 true
-
contains
public static boolean contains(int[] array, int element) Returns true if an int array contains the specified element. 检查 int 数组是否包含指定元素 -
indexOf
public static <T> int indexOf(T[] array, T element) Returns the index of the first occurrence of the element in the array. 查找元素索引- Type Parameters:
T- element type | 元素类型- Parameters:
array- the array | 数组element- element to search for | 元素- Returns:
- the index, or -1 if not found | 索引,未找到返回 -1
-
indexOf
public static <T> int indexOf(T[] array, T element, int startIndex) Returns the index of the first occurrence of the element starting from startIndex. 从指定位置开始查找元素索引- Type Parameters:
T- element type | 元素类型- Parameters:
array- the array | 数组element- element to search for | 元素startIndex- start index | 起始索引- Returns:
- the index, or -1 if not found | 索引,未找到返回 -1
-
lastIndexOf
public static <T> int lastIndexOf(T[] array, T element) Returns the index of the last occurrence of the element in the array. 从后向前查找元素索引- Type Parameters:
T- element type | 元素类型- Parameters:
array- the array | 数组element- element to search for | 元素- Returns:
- the index, or -1 if not found | 索引,未找到返回 -1
-
getFirst
Returns the first element of the array wrapped in an Optional. 获取数组第一个元素- Type Parameters:
T- element type | 元素类型- Parameters:
array- the array | 数组- Returns:
- Optional wrapping the first element | Optional 包装的第一个元素
-
getLast
Returns the last element of the array wrapped in an Optional. 获取数组最后一个元素- Type Parameters:
T- element type | 元素类型- Parameters:
array- the array | 数组- Returns:
- Optional wrapping the last element | Optional 包装的最后一个元素
-
isEmpty
Returns true if the array is null or has length zero. 检查数组是否为空- Parameters:
array- the array | 数组- Returns:
- true if null or empty | 如果为 null 或长度为 0 返回 true
-
isEmpty
public static boolean isEmpty(int[] array) Returns true if an int array is null or has length zero. 检查 int 数组是否为空 -
isNotEmpty
Returns true if the array is not null and has at least one element. 检查数组是否非空 -
isSameLength
-
isSorted
public static boolean isSorted(int[] array) Returns true if the int array is sorted in ascending order. 检查 int 数组是否已排序- Parameters:
array- the array | 数组- Returns:
- true if sorted in ascending order | 如果已升序排序返回 true
-
isSorted
Returns true if the Comparable array is sorted in ascending order. 检查 Comparable 数组是否已排序- Type Parameters:
T- element type | 元素类型- Parameters:
array- the array | 数组- Returns:
- true if sorted in ascending order | 如果已升序排序返回 true
-
toPrimitive
Converts an Integer array to an int array. Integer 数组转 int 数组 -
toPrimitive
Converts an Integer array to an int array with a specified value for null elements. Integer 数组转 int 数组(指定 null 替代值) -
toPrimitive
Converts a Long array to a long array. Long 数组转 long 数组 -
toPrimitive
Converts a Double array to a double array. Double 数组转 double 数组 -
toPrimitive
Converts a Boolean array to a boolean array. Boolean 数组转 boolean 数组 -
toPrimitive
Converts a Byte array to a byte array. Byte 数组转 byte 数组 -
toPrimitive
Converts a Character array to a char array. Character 数组转 char 数组 -
toObject
Converts an int array to an Integer array. int 数组转 Integer 数组 -
toObject
Converts a long array to a Long array. long 数组转 Long 数组 -
toObject
Converts a double array to a Double array. double 数组转 Double 数组 -
toObject
Converts a boolean array to a Boolean array. boolean 数组转 Boolean 数组 -
toObject
Converts a byte array to a Byte array. byte 数组转 Byte 数组 -
toObject
Converts a char array to a Character array. char 数组转 Character 数组 -
toList
Converts an array to a mutable List. 数组转 List- Type Parameters:
T- element type | 元素类型- Parameters:
array- elements | 数组- Returns:
- mutable List | 可变 List
-
toSet
Converts an array to a Set. 数组转 Set- Type Parameters:
T- element type | 元素类型- Parameters:
array- elements | 数组- Returns:
- Set containing all elements | Set
-
toMap
Converts a two-dimensional array to a Map. Each sub-array must contain at least two elements: [key, value]. 二维数组转 Map每个子数组必须包含至少两个元素:[key, value]
- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
array- two-dimensional array | 二维数组- Returns:
- Map | Map
-
filter
Filters the array, returning a new array containing only elements matching the predicate. 过滤数组- Type Parameters:
T- element type | 元素类型- Parameters:
array- the array | 数组predicate- filter condition | 过滤条件- Returns:
- filtered array | 过滤后的数组
-
map
Maps each element of the array using the given mapper function. 映射数组- Type Parameters:
T- source element type | 原元素类型R- target element type | 目标元素类型- Parameters:
array- original array | 原数组mapper- mapping function | 映射函数targetType- target element type | 目标类型- Returns:
- mapped array | 映射后的数组
-