Class Ints
java.lang.Object
cloud.opencode.base.core.primitives.Ints
Int Array Utility Class - Guava-style operations for int primitive arrays
int 数组工具类 - Guava 风格的 int 原始类型数组操作
Provides comprehensive int array operations inspired by Guava Ints.
提供 int 原始类型数组的操作方法,参考 Guava Ints。
Features | 主要功能:
- Byte conversion (toByteArray, fromByteArray, fromBytes) - 字节转换
- Array operations (concat, contains, indexOf, lastIndexOf) - 数组操作
- Min/Max (min, max, constrainToRange) - 最值和范围
- Safe casting (saturatedCast, checkedCast) - 安全转换
- Collection conversion (asList, toArray) - 集合转换
- Array manipulation (reverse, rotate, sortDescending) - 数组操作
- Comparison (compare, lexicographicalComparator) - 比较
- String operations (join, tryParse) - 字符串操作
Usage Examples | 使用示例:
// Byte conversion - 字节转换
byte[] bytes = Ints.toByteArray(12345);
int value = Ints.fromByteArray(bytes);
// Array operations - 数组操作
int[] merged = Ints.concat(arr1, arr2);
int idx = Ints.indexOf(arr, 5);
// Safe conversion - 安全转换
int clamped = Ints.saturatedCast(Long.MAX_VALUE); // Integer.MAX_VALUE
Security | 安全性:
- Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
- Null-safe: Partially (throws on null array) - 空值安全: 部分 (null 数组抛异常)
- Since:
- JDK 25, opencode-base-core V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final int[]Empty array constant 空数组常量static final int -
Method Summary
Modifier and TypeMethodDescriptionasList(int... array) Converts to List 转为 Liststatic intcheckedCast(long value) Checked conversion (long to int, throws on overflow) 检查转换(long 转 int,溢出则抛异常)static intcompare(int a, int b) Compares two int values 比较两个 int 值static int[]concat(int[]... arrays) Concatenates multiple arrays 合并多个数组static intconstrainToRange(int value, int min, int max) Constrains the value within the specified range 约束值在指定范围内static booleancontains(int[] array, int target) Checks if the array contains the specified element 检查数组是否包含指定元素static int[]ensureCapacity(int[] array, int minLength, int padding) Ensures that array has minimum capacity, expanding if needed.static int[]ensureNonNull(int[] array) Ensures the array is not null 确保数组不为 nullstatic intfromByteArray(byte[] bytes) Converts byte array to int (big-endian) byte 数组转 int(大端序)static intfromBytes(byte b1, byte b2, byte b3, byte b4) Converts 4 bytes to int 4 个字节转 intstatic intindexOf(int[] array, int target) Finds the element index 查找元素索引static intindexOf(int[] array, int[] target) Finds the position of a sub-array in the array 查找子数组在数组中的位置static intindexOf(int[] array, int target, int start, int end) Finds the element index within the specified range 在指定范围内查找元素索引static booleanisSorted(int[] array) Checks if the array is sorted 检查数组是否已排序static StringConverts the array to string 数组转字符串static intlastIndexOf(int[] array, int target) Finds the element index from the end 从后向前查找元素索引static intlastIndexOf(int[] array, int target, int start, int end) Finds the element index from the end within the specified range 在指定范围内从后向前查找元素索引static Comparator<int[]> Gets the comparator 获取比较器static intmax(int... array) Returns the maximum value 返回最大值static intmin(int... array) Returns the minimum value 返回最小值static voidreverse(int[] array) Reverses the array 反转数组static voidreverse(int[] array, int fromIndex, int toIndex) Reverses the specified range of the array 反转数组指定范围static voidrotate(int[] array, int distance) Rotates the array 旋转数组static intsaturatedCast(long value) Saturated conversion (long to int) 饱和转换(long 转 int)static voidsortDescending(int[] array) Sorts in descending order 降序排序static voidsortDescending(int[] array, int fromIndex, int toIndex) Sorts the specified range in descending order 降序排序指定范围static int[]toArray(Collection<? extends Number> collection) Converts from Collection to array 从 Collection 转为数组static byte[]toByteArray(int value) Converts int to byte array (big-endian) int 转 byte 数组(大端序)static IntegerAttempts to parse as int 尝试解析为 intstatic IntegerAttempts to parse as int (specified radix) 尝试解析为 int(指定进制)
-
Field Details
-
BYTES
public static final int BYTES- See Also:
-
MAX_POWER_OF_TWO
public static final int MAX_POWER_OF_TWO- See Also:
-
EMPTY_ARRAY
public static final int[] EMPTY_ARRAYEmpty array constant 空数组常量
-
-
Method Details
-
toByteArray
public static byte[] toByteArray(int value) Converts int to byte array (big-endian) int 转 byte 数组(大端序) -
fromByteArray
public static int fromByteArray(byte[] bytes) Converts byte array to int (big-endian) byte 数组转 int(大端序) -
fromBytes
public static int fromBytes(byte b1, byte b2, byte b3, byte b4) Converts 4 bytes to int 4 个字节转 int -
concat
public static int[] concat(int[]... arrays) Concatenates multiple arrays 合并多个数组 -
contains
public static boolean contains(int[] array, int target) Checks if the array contains the specified element 检查数组是否包含指定元素 -
indexOf
public static int indexOf(int[] array, int target) Finds the element index 查找元素索引 -
indexOf
public static int indexOf(int[] array, int target, int start, int end) Finds the element index within the specified range 在指定范围内查找元素索引 -
indexOf
public static int indexOf(int[] array, int[] target) Finds the position of a sub-array in the array 查找子数组在数组中的位置- Parameters:
array- the source array | 源数组target- the target sub-array | 目标子数组- Returns:
- the result | 子数组首次出现的索引,未找到返回 -1
-
lastIndexOf
public static int lastIndexOf(int[] array, int target) Finds the element index from the end 从后向前查找元素索引 -
lastIndexOf
public static int lastIndexOf(int[] array, int target, int start, int end) Finds the element index from the end within the specified range 在指定范围内从后向前查找元素索引 -
min
public static int min(int... array) Returns the minimum value 返回最小值 -
max
public static int max(int... array) Returns the maximum value 返回最大值 -
constrainToRange
public static int constrainToRange(int value, int min, int max) Constrains the value within the specified range 约束值在指定范围内 -
saturatedCast
public static int saturatedCast(long value) Saturated conversion (long to int) 饱和转换(long 转 int) -
checkedCast
public static int checkedCast(long value) Checked conversion (long to int, throws on overflow) 检查转换(long 转 int,溢出则抛异常) -
compare
public static int compare(int a, int b) Compares two int values 比较两个 int 值 -
lexicographicalComparator
Gets the comparator 获取比较器 -
asList
-
toArray
Converts from Collection to array 从 Collection 转为数组 -
reverse
public static void reverse(int[] array) Reverses the array 反转数组 -
reverse
public static void reverse(int[] array, int fromIndex, int toIndex) Reverses the specified range of the array 反转数组指定范围 -
rotate
public static void rotate(int[] array, int distance) Rotates the array 旋转数组正数向右旋转,负数向左旋转
- Parameters:
array- the array | 数组distance- rotation distance | 旋转距离
-
sortDescending
public static void sortDescending(int[] array) Sorts in descending order 降序排序 -
sortDescending
public static void sortDescending(int[] array, int fromIndex, int toIndex) Sorts the specified range in descending order 降序排序指定范围 -
isSorted
public static boolean isSorted(int[] array) Checks if the array is sorted 检查数组是否已排序 -
join
-
tryParse
-
tryParse
-
ensureNonNull
public static int[] ensureNonNull(int[] array) Ensures the array is not null 确保数组不为 null -
ensureCapacity
public static int[] ensureCapacity(int[] array, int minLength, int padding) Ensures that array has minimum capacity, expanding if needed. 确保数组具有最小容量,如果需要则扩展。- Parameters:
array- the original array | 原数组minLength- the minimum required length | 最小需要长度padding- additional padding beyond minLength | 超出 minLength 的额外填充- Returns:
- the array with ensured capacity | 确保容量后的数组
-