# 数组相关操作
java.util.Arrays
Java JDK 在 java.util 包下提供了一个 Arrays 工具类,用于对数组的各种常见操作。
org.springframework.util.ObjectUtils
Spring 框架在 org.springframework.util 包下提供了一个 ObjectUtils 工具类。虽然,它不是专用于数组的工具类,但是其中有几个方法可用于数据。
# 1. 数组判断空
# 1.1 是否是数组
isArray 方法来源于 Spring 框架的 ObjectUtils 工具类。
public static boolean isArray(Object obj)
伪代码示例如下:
isArray(null) = false
isArray([]) = true
isArray([1,2,3]) = true
# 1.2 数组判空
isEmpty 方法来源于 Spring 框架的 ObjectUtils 工具类。
public static boolean isEmpty(Object array)
public static boolean isEmpty(Object[] array)
伪代码示例如下:
isEmpty(null) = true
isEmpty([]) = true
isEmpty([1,2,3]) = false
# 2. 数组的内容查询
# 2.1 二分查找
binarySearch 方法来自于 java.util.Arrays 工具类。
使用它有前提:要求数组是有序的。
public static int binarySearch(int[] a, int key)
public static int binarySearch(long[] a, int key)
...
binarySearch 方法的返回值是元素在数组中的索引值。
binarySearch([0,1,2,3,4,5], 3) = 3
binarySearch([5,4,3,2,1,0], 3) = 2
# 2.2 是否包含 xxx
containsElement 方法来源于 Spring 框架的 ObjectUtils 工具类。
public static boolean containsElement(Object[] array, Object element)
它只能用于对象数组,而不能用于基本类型数组。
containsElement([1,2,3], 1) = true
containsElement([1,2,3], 4) = false
containsElement([1,2,3], null)= false
# 3. 填充与拷贝
# 3.1 添加
addObjectToArray 方法来自于 Spring 框架的 ObjectUtils 工具类。
向数组的末尾 “追加” 新元素。这里的 “追加” 是假追加,原数组并没有发生变化(也不可能发生变化),这里返回的是一个新数组。
// 向参数数组的末尾追加新元素,并返回一个新数组。
<A, O extends A> A[] addObjectToArray(A[] array, O obj)
# 3.2 填充(赋值)
fill 方法来源于 JDK 自带的 Arrays 工具类。
public static void fill(int[] a, int val)
public static void fill(int[] a, int fromIndex, int toIndex, int val);
...
public static void fill(long[] a, long val)
public static void fill(long[] a, int fromIndex, int toIndex, long val);
...
fill 方法改变的是原数组。
fromIndex 和 toIndex 是一个左闭右开区间:[fromIndex, toIndex)
。
伪代码示例:
fill([1,2,3], 4) = [4, 4, 4]
fill([1,2,3], 1, 2, 4) = [1, 4, 3]
# 3.2 拷贝
copyOf / copyOfRange 方法来源于 JDK 自带的 Arrays 工具类。
public static int[] copyOf(int[] original, int newLength)
public static long[] copyOf(long[] original, int newLength)
...
public static int[] copyOfRange(int[] original, int from, int to)
public static long[] copyOfRange(long[] original, int from, int to)
...
from 和 to 是一个左闭右开的区间:[from, to) 。
Arrays.copyOf([0,1,2,3,4], 2)) = [0, 1]
Arrays.copyOfRange([0,1,2,3,4], 2, 4)) = [2, 3]
# 4. 数组转换
# 转成 List
asList 方法来源于 JDK 自带的 Arrays 工具类。
由给定的数组 a ,返回一个固定大小的 List 对象。
public static <T> List<T> asList(T... a);
这里返回的 ArrayList 并不是 java.util.ArrayList
,而是 Arrays 的内部类 ArrayList 。它不支持 add 和 remove 的操作,是只读的。另外,当改变原数组中的元素时,这个 ArrayList 会 “自动” 随之变化。
逻辑上,这里返回的 ArrayList 对象是数组的一个 “视图” 。
伪代码示例如下:
asList([1,2,3]) = [1,2,3]
Spring 框架中的 CollectionUtils 的 arrayToList 方法也能实现数组转集合的功能。
List arrayToList(Object source)
# 转成 String
toString / deepToString 方法来自于 JDK 自带的 Arrays 工具类。
public static String toString(int[] a)
public static String toString(long[] a)
...
// 可用于二维数组
public static String deepToString(int[] a);
public static String deepToString(long[] a);
...
伪代码示例如下:
toString(null) = "null"
toString([1]) = "[1]"
toString([1,2,3]) = "[1, 2, 3]"
deepToString(null) = "null"
deepToString([[1,2],[3,4]]) = "[[1, 2], [3, 4]]"
# 基本类型数组转包装类数组
// 原生基础类型数组 --> 包装类数组
Object[] toObjectArray(Object source)
# 5. 排序
sort 方法来源于 JDK 自带的 Arrays 工具类。
public static void sort(int[] a)
sort 方法改变的是原数组(源数组)。
伪代码示例如下:
sort(null) = NPE
sort([4,3,2,1,0]) = [0,1,2,3,4]