Class ArrayUtil

java.lang.Object
com.sure.tool.util.ArrayUtil

public class ArrayUtil extends Object
数组工具类。

支持对象数组与基本类型数组的统一处理,参考 Hutool 的 ArrayUtil 设计。

English: Array creation, concatenation, containment and conversion helpers.

Since:
0.1.0
Author:
suretool
  • Method Summary

    Modifier and Type
    Method
    Description
    static Object
    append(Object array, Object... values)
    追加元素(返回新数组)。
    static boolean
    contains(Object array, Object value)
    数组中是否包含指定元素。
    static Object[]
    数组去重(保持原有顺序)。
    static <T> T[]
    distinct(T[] array)
    数组去重(保持首次出现顺序)。
    static <T> T[]
    empty(Class<T> componentType)
    创建指定类型的空数组。
    static <T> T
    firstNonNull(T... array)
    返回第一个非 null 的元素。
    static Object
    get(Object array, int index)
    获取数组中指定索引的元素。
    static Object
    get(Object array, int index, Object defaultValue)
    获取数组中指定索引的元素,越界时返回默认值。
    static <T> T
    get(T[] array, int index)
    安全读取数组元素,越界或 null 返回 null。
    static <T> T
    get(T[] array, int index, T defaultValue)
    安全读取数组元素,越界或 null 返回默认值。
    static int
    indexOf(Object array, Object value)
    查找元素在数组中的索引。
    static Object
    insert(Object array, int index, Object... values)
    指定位置插入元素(返回新数组)。
    static boolean
    对象是否为数组。
    static boolean
    isEmpty(Object array)
    数组是否为空。
    static boolean
    数组是否非空。
    static <T extends Comparable<T>>
    boolean
    isSorted(T[] array)
    判断数组是否升序(自然顺序)。
    static <T extends Comparable<T>>
    boolean
    isSorted(T[] array, boolean asc)
    判断数组是否有序。
    static String
    join(Object array, CharSequence delimiter)
    使用分隔符拼接数组。
    static int
    lastIndexOf(Object array, Object value)
    末次出现位置。
    static int
    length(Object array)
    获取数组长度。
    static double
    max(double[] array)
    double 数组最大值。
    static int
    max(int[] array)
    int 数组最大值。
    static long
    max(long[] array)
    long 数组最大值。
    static <T extends Comparable<? super T>>
    T
    max(T[] array)
    对象数组最大值。
    static double
    min(double[] array)
    double 数组最小值。
    static int
    min(int[] array)
    int 数组最小值。
    static long
    min(long[] array)
    long 数组最小值。
    static <T extends Comparable<? super T>>
    T
    min(T[] array)
    对象数组最小值。
    static <T> T[]
    nullToEmpty(T[] array, Class<T> componentType)
    null 数组转换为指定类型的空数组。
    static Object
    remove(Object array, Object value)
    移除首个匹配元素(返回新数组)。
    static <T> T[]
    resize(T[] array, int newSize)
    调整数组长度(扩容补 null,缩容截断)。
    static void
    reverse(Object array)
    反转数组。
    static Object[]
    sub(Object array, int from, int to)
    截取数组子区间。
    static void
    swap(Object array, int i, int j)
    交换数组中两位置元素(原数组修改)。
    static <T> T[]
    toArray(Collection<T> collection, Class<T> clazz)
    集合转数组。
    static List<Object>
    toList(Object array)
    数组转 List(支持基本类型数组,自动装箱)。
    static String
    对象数组转字符串。
    static Boolean[]
    wrap(boolean[] array)
    基本类型数组转包装类型数组。
    static Byte[]
    wrap(byte[] array)
    基本类型数组转包装类型数组。
    static Character[]
    wrap(char[] array)
    基本类型数组转包装类型数组。
    static Double[]
    wrap(double[] array)
    基本类型数组转包装类型数组。
    static Float[]
    wrap(float[] array)
    基本类型数组转包装类型数组。
    static Integer[]
    wrap(int[] array)
    基本类型数组转包装类型数组。
    static Long[]
    wrap(long[] array)
    基本类型数组转包装类型数组。
    static Short[]
    wrap(short[] array)
    基本类型数组转包装类型数组。

    Methods inherited from class Object

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

    • isArray

      public static boolean isArray(Object obj)
      对象是否为数组。
      Parameters:
      obj - 对象
      Returns:
      是否为数组
    • isEmpty

      public static boolean isEmpty(Object array)
      数组是否为空。
      Parameters:
      array - 数组(支持基本类型数组)
      Returns:
      是否为空
    • isNotEmpty

      public static boolean isNotEmpty(Object array)
      数组是否非空。
      Parameters:
      array - 数组(支持基本类型数组)
      Returns:
      是否非空
    • length

      public static int length(Object array)
      获取数组长度。
      Parameters:
      array - 数组(支持基本类型数组)
      Returns:
      长度,非数组返回 0
    • contains

      public static boolean contains(Object array, Object value)
      数组中是否包含指定元素。
      Parameters:
      array - 数组(支持基本类型数组)
      value - 元素
      Returns:
      是否包含
    • indexOf

      public static int indexOf(Object array, Object value)
      查找元素在数组中的索引。
      Parameters:
      array - 数组(支持基本类型数组)
      value - 元素
      Returns:
      索引,未找到返回 -1
    • get

      public static Object get(Object array, int index)
      获取数组中指定索引的元素。
      Parameters:
      array - 数组(支持基本类型数组)
      index - 索引,负数表示从末尾倒数
      Returns:
      元素,越界返回 null
    • get

      public static Object get(Object array, int index, Object defaultValue)
      获取数组中指定索引的元素,越界时返回默认值。
      Parameters:
      array - 数组(支持基本类型数组)
      index - 索引,负数表示从末尾倒数
      defaultValue - 默认值
      Returns:
      元素或默认值
    • join

      public static String join(Object array, CharSequence delimiter)
      使用分隔符拼接数组。
      Parameters:
      array - 数组(支持基本类型数组)
      delimiter - 分隔符
      Returns:
      拼接结果
    • toList

      public static List<Object> toList(Object array)
      数组转 List(支持基本类型数组,自动装箱)。
      Parameters:
      array - 数组
      Returns:
      List
    • toArray

      public static <T> T[] toArray(Collection<T> collection, Class<T> clazz)
      集合转数组。
      Type Parameters:
      T - 元素类型
      Parameters:
      collection - 集合
      clazz - 数组元素类型
      Returns:
      数组
    • reverse

      public static void reverse(Object array)
      反转数组。
      Parameters:
      array - 数组(支持基本类型数组),原地反转
    • distinct

      public static Object[] distinct(Object array)
      数组去重(保持原有顺序)。
      Parameters:
      array - 数组(支持基本类型数组)
      Returns:
      去重后的对象数组
    • sub

      public static Object[] sub(Object array, int from, int to)
      截取数组子区间。
      Parameters:
      array - 数组(支持基本类型数组)
      from - 起始索引(含)
      to - 结束索引(不含)
      Returns:
      子数组(对象数组)
    • firstNonNull

      @SafeVarargs public static <T> T firstNonNull(T... array)
      返回第一个非 null 的元素。
      Type Parameters:
      T - 元素类型
      Parameters:
      array - 数组
      Returns:
      第一个非空元素
    • min

      public static <T extends Comparable<? super T>> T min(T[] array)
      对象数组最小值。
      Type Parameters:
      T - 元素类型
      Parameters:
      array - 数组
      Returns:
      最小值
    • max

      public static <T extends Comparable<? super T>> T max(T[] array)
      对象数组最大值。
      Type Parameters:
      T - 元素类型
      Parameters:
      array - 数组
      Returns:
      最大值
    • min

      public static int min(int[] array)
      int 数组最小值。
      Parameters:
      array - 数组
      Returns:
      最小值,空数组返回 0
    • max

      public static int max(int[] array)
      int 数组最大值。
      Parameters:
      array - 数组
      Returns:
      最大值,空数组返回 0
    • min

      public static long min(long[] array)
      long 数组最小值。
      Parameters:
      array - 数组
      Returns:
      最小值,空数组返回 0
    • max

      public static long max(long[] array)
      long 数组最大值。
      Parameters:
      array - 数组
      Returns:
      最大值,空数组返回 0
    • min

      public static double min(double[] array)
      double 数组最小值。
      Parameters:
      array - 数组
      Returns:
      最小值,空数组返回 0
    • max

      public static double max(double[] array)
      double 数组最大值。
      Parameters:
      array - 数组
      Returns:
      最大值,空数组返回 0
    • toString

      public static String toString(Object array)
      对象数组转字符串。
      Parameters:
      array - 数组
      Returns:
      字符串
    • lastIndexOf

      public static int lastIndexOf(Object array, Object value)
      末次出现位置。
      Parameters:
      array - 数组
      value - 值
      Returns:
      位置;未找到返回 -1
    • swap

      public static void swap(Object array, int i, int j)
      交换数组中两位置元素(原数组修改)。
      Parameters:
      array - 数组
      i - 位置一
      j - 位置二
    • remove

      public static Object remove(Object array, Object value)
      移除首个匹配元素(返回新数组)。
      Parameters:
      array - 数组
      value - 要移除的值
      Returns:
      新数组
    • append

      public static Object append(Object array, Object... values)
      追加元素(返回新数组)。
      Parameters:
      array - 原数组
      values - 追加值
      Returns:
      新数组
    • insert

      public static Object insert(Object array, int index, Object... values)
      指定位置插入元素(返回新数组)。
      Parameters:
      array - 原数组
      index - 插入位置(0~length)
      values - 插入值
      Returns:
      新数组
    • wrap

      public static Integer[] wrap(int[] array)
      基本类型数组转包装类型数组。
      Parameters:
      array - 基本类型数组
      Returns:
      包装类型数组
    • wrap

      public static Long[] wrap(long[] array)
      基本类型数组转包装类型数组。
      Parameters:
      array - 基本类型数组
      Returns:
      包装类型数组
    • wrap

      public static Double[] wrap(double[] array)
      基本类型数组转包装类型数组。
      Parameters:
      array - 基本类型数组
      Returns:
      包装类型数组
    • wrap

      public static Float[] wrap(float[] array)
      基本类型数组转包装类型数组。
      Parameters:
      array - 基本类型数组
      Returns:
      包装类型数组
    • wrap

      public static Short[] wrap(short[] array)
      基本类型数组转包装类型数组。
      Parameters:
      array - 基本类型数组
      Returns:
      包装类型数组
    • wrap

      public static Byte[] wrap(byte[] array)
      基本类型数组转包装类型数组。
      Parameters:
      array - 基本类型数组
      Returns:
      包装类型数组
    • wrap

      public static Character[] wrap(char[] array)
      基本类型数组转包装类型数组。
      Parameters:
      array - 基本类型数组
      Returns:
      包装类型数组
    • wrap

      public static Boolean[] wrap(boolean[] array)
      基本类型数组转包装类型数组。
      Parameters:
      array - 基本类型数组
      Returns:
      包装类型数组
    • isSorted

      public static <T extends Comparable<T>> boolean isSorted(T[] array)
      判断数组是否升序(自然顺序)。
      Type Parameters:
      T - 元素类型
      Parameters:
      array - 数组
      Returns:
      是否升序
    • isSorted

      public static <T extends Comparable<T>> boolean isSorted(T[] array, boolean asc)
      判断数组是否有序。
      Type Parameters:
      T - 元素类型
      Parameters:
      array - 数组
      asc - 是否升序
      Returns:
      是否有序
    • nullToEmpty

      public static <T> T[] nullToEmpty(T[] array, Class<T> componentType)
      null 数组转换为指定类型的空数组。
      Type Parameters:
      T - 元素类型
      Parameters:
      array - 数组
      componentType - 数组元素类型
      Returns:
      原数组;null 时返回空数组
    • resize

      public static <T> T[] resize(T[] array, int newSize)
      调整数组长度(扩容补 null,缩容截断)。
      Type Parameters:
      T - 元素类型
      Parameters:
      array - 原数组
      newSize - 新长度
      Returns:
      新数组
    • get

      public static <T> T get(T[] array, int index)
      安全读取数组元素,越界或 null 返回 null。
      Type Parameters:
      T - 元素类型
      Parameters:
      array - 数组
      index - 索引
      Returns:
      元素或 null
    • get

      public static <T> T get(T[] array, int index, T defaultValue)
      安全读取数组元素,越界或 null 返回默认值。
      Type Parameters:
      T - 元素类型
      Parameters:
      array - 数组
      index - 索引
      defaultValue - 默认值
      Returns:
      元素或默认值
    • distinct

      public static <T> T[] distinct(T[] array)
      数组去重(保持首次出现顺序)。
      Type Parameters:
      T - 元素类型
      Parameters:
      array - 数组
      Returns:
      去重后数组;原数组为 null 返回 null
    • empty

      public static <T> T[] empty(Class<T> componentType)
      创建指定类型的空数组。
      Type Parameters:
      T - 元素类型
      Parameters:
      componentType - 数组元素类型
      Returns:
      空数组