java.util.Arrays
概要
Arrays类主要提供了数组的各种操作,是个纯工具类
public class Arrays extends Object
常用方法
- asList 快速根据可变参数(数组)生成一个List
public static List asList(T... a)
栗子
List list = Arrays.asList("Text", 1, new Date());
System.out.println(list);
- toString 快速将数组格式化成字符串,格式为"[item1,item2]"
实质:StringBuilder+String.valueOf+[]+,组合而成
public static String toString(Object[] a)
public static String deepToString(Object[] a) 递归合并(数组嵌套数组)
栗子
String[] strs = new String[]{"A", "B", "C"};
System.out.println( Arrays.toString(strs));
Object[] deepStrs = new Object[]{"A", "B", "C",strs};
System.out.println( Arrays.deepToString(deepStrs));
- equals 比较两个数组是否相等
public static boolean equals(Object[] a, Object[] a2)
public static boolean deepEquals(Object[] a1, Object[] a2) //递归
- fill 填充数组
public static void fill(Object[] a, Object val)
public static void fill(Object[] a, int fromIndex, int toIndex, Object val)
- copyOf 拷贝数组
public static T[] copyOf(T[] original, int newLength)
public static T[] copyOf(U[] original, int newLength, Class extends T[]> newType)
public static T[] copyOfRange(T[] original, int from, int to)
public static T[] copyOfRange(U[] original, int from, int to, Class extends T[]> newType
- sort 数组排序
排序的算法从1.7开始根据不同的数组长度自动采用不同的算法
public static void sort(T[] a, Comparator super T> c)
public static void sort(T[] a, int fromIndex, int toIndex, Comparator super T> c)
栗子
int[] data = new int[]{99,1,9,4,6,0};
Arrays.sort(data);
System.out.println(Arrays.toString(data));
结果:[0, 1, 4, 6, 9, 99]
- binarySearch 提供了各种变种方法,实质二分法查找,所以使用前提是有序数组
支持简单搜索、范围搜索、自定义比较器
@param a 数组
@param fromIndex 从第几位开始 (包括)
@param toIndex 到第几位结束 (不包括)
@param key 要检索的关键字
@param c 自定义比较器
@return 关键字的位置 ,否则返回 -(fromIndex+1)
public static int binarySearch(T[] a, T key)
public static int binarySearch(T[] a, int fromIndex, int toIndex, T key)
public static int binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator super T> c)
栗子
int item = 11;
int[] arr = new int[]{0,1, 2,item, 3, 4, 5, 6, 7, 8,9,10};
Arrays.sort(arr);
int index = Arrays.binarySearch(arr, item);
System.out.println(String.format("in %s search %d find at %d",Arrays.toString(arr),item,index));
- parallelPrefix (1.8新增) 用户对相邻数据做变换
public static void parallelPrefix(T[] array, BinaryOperator op)
public static void parallelPrefix(T[] array, int fromIndex, int toIndex, BinaryOperator op)
BinaryOperator
@param left 前一次操作的结果
@param right 当前数组中的值
@return R 返回操作结果
R apply(T left, U right);
栗子
int[] srcArr = new int[]{0, 1, 2, 3, 4, 5};
Arrays.parallelPrefix(srcArr, (left, right) -> left+right);
System.out.println(Arrays.toString(srcArr));
[0, 1, 3, 6, 10, 15] = (0,0+1,1+2,3+3,6+4,10+5)
- stream (1.8新增) 将数组Stream化,Stream支持fitler,map,sort等操作。
public static Stream stream(T[] array)
public static Stream stream(T[] array, int startInclusive, int endExclusive)
栗子
Arrays.stream(new int[]{1,2,3,4,5,6})
.filter(value -> value <= 3)
.forEach(value -> System.out.println(value));
结果:1 2 3
- spliterator(1.8新增) 将数组spliterator化,Spliterator支持元素的遍历和截取等操作
public static Spliterator spliterator(T[] array)
public static Spliterator spliterator(T[] array, int startInclusive, int endExclusive)
栗子
Arrays.spliterator(new int[]{1,2,3,4,5,6}).forEachRemaining((IntConsumer) value -> System.out.println(value));
结果:1 2 3 4 5 6