文章目录
- 方法描述
- 方法举例
- 1. asList
- 2. binarySearch
- 3. sort
- 4. parallelSort
- 5. fill
- 6. copyOf
- 7. copyOfRange
- 8. setAll
- 9. parallelSetAll
- 10. parallelPrefix
- 11. spliterator
- 12. stream
- 13. equals
- 14. deepEquals
- 15. hashCode
- 16. deepHashCode
- 17. toString
- 18. deepToString
方法描述
方法名称 | 方法类型 | 具体方法 | 方法描述 |
asList | 转List | asList(T... a) | 将数组或者是可变参数转化为List |
binarySearch | 二分查找 | binarySearch(byte[] a, byte key) binarySearch(byte[] a, int fromIndex, int toIndex, byte key) binarySearch(char[] a, char key) binarySearch(char[] a, int fromIndex, int toIndex, char key) binarySearch(short[] a, short key) binarySearch(short[] a, int fromIndex, int toIndex, short key) binarySearch(int[] a, int key) binarySearch(int[] a, int fromIndex, int toIndex, int key) binarySearch(long[] a, long key) binarySearch(long[] a, int fromIndex, int toIndex, long key) binarySearch(float[] a, float key) binarySearch(float[] a, int fromIndex, int toIndex, float key) binarySearch(double[] a, double key) binarySearch(double[] a, int fromIndex, int toIndex, double key) binarySearch(Object[] a, Object key) binarySearch(Object[] a, int fromIndex, int toIndex, Object key) binarySearch(T[] T key, Comparator<? super T> c) binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c) | 查找指定key在数组中的下标(找不到返回负数(-插入点-1)) (使用二分查找,数组一定要是升序排好序的,否则会出错) |
sort | 排序 | sort(byte[] a) sort(byte[] a, int fromIndex, int toIndex) sort(char[] a) sort(char[] a, int fromIndex, int toIndex) sort(short[] a) sort(short[] a, int fromIndex, int toIndex) sort(int[] a) sort(int[] a, int fromIndex, int toIndex) sort(long[] a) sort(long[] a, int fromIndex, int toIndex) sort(float[] a) sort(float[] a, int fromIndex, int toIndex) sort(double[] a) sort(double[] a, int fromIndex, int toIndex) sort(Object[] a) sort(Object[] a, int fromIndex, int toIndex) sort(T[] a, Comparator<? super T> c) sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c) | 对数组中的元素进行升序排序。 |
parallelSort | 并行排序 | parallelSort(byte[] a) parallelSort(byte[] a, int fromIndex, int toIndex) parallelSort(char[] a) parallelSort(char[] a, int fromIndex, int toIndex) parallelSort(short[] a) parallelSort(short[] a, int fromIndex, int toIndex) parallelSort(int[] a) parallelSort(int[] a, int fromIndex, int toIndex) parallelSort(long[] a) parallelSort(long[] a, int fromIndex, int toIndex) parallelSort(float[] a) parallelSort(float[] a, int fromIndex, int toIndex) parallelSort(double[] a) parallelSort(double[] a, int fromIndex, int toIndex) parallelSort(T[] a) parallelSort(T[] a, Comparator<? super T> c) parallelSort(T[] a, int fromIndex, int toIndex) parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c) | 对数组中的元素进行升序排序(并行)。 |
fill | 填充元素 | fill(byte[] a, byte val) fill(byte[] a, int fromIndex, int toIndex, byte val) fill(char[] a, char val) fill(char[] a, int fromIndex, int toIndex, char val) fill(short[] a, short val) fill(short[] a, int fromIndex, int toIndex, short val) fill(int[] a, int val) fill(int[] a, int fromIndex, int toIndex, int val) fill(long[] a, long val) fill(long[] a, int fromIndex, int toIndex, long val) fill(float[] a, float val) fill(float[] a, int fromIndex, int toIndex, float val) fill(double[] a, double val) fill(double[] a, int fromIndex, int toIndex,double val) fill(boolean[] a, boolean val) fill(boolean[] a, int fromIndex, int toIndex, boolean val) fill(Object[] a, Object val) fill(Object[] a, int fromIndex, int toIndex, Object val) | 用指定元素替换数组中的所有元素 |
copyOf | 拷贝数组 | copyOf(byte[] original, int newLength) copyOf(char[] original, int newLength) copyOf(short[] original, int newLength) copyOf(int[] original, int newLength) copyOf(long[] original, int newLength) copyOf(float[] original, int newLength) copyOf(double[] original, int newLength) copyOf(boolean[] original, int newLength) copyOf(T[] original, int newLength) copyOf(U[] original, int newLength, Class<? extends T[]> newType) | 把数组复制到另一个数组,并指定新数组的长度,如果新数组的长度大于要复制的数组的长度,空的位置默认值来填充。 |
copyOfRange | 拷贝数组 (指定范围) | copyOf(byte[] original, int from, int to) copyOf(char[] original, int from, int to) copyOf(short[] original, int from, int to) copyOf(int[] original, int from, int to) copyOf(long[] original, int from, int to) copyOf(float[] original, int from, int to) copyOf(double[] original, int from, int to) copyOf(boolean[] original, int from, int to) copyOf(T[] original, int from, int to) copyOf(U[] original, int from, int to, Class<? extends T[]> newType) | 把数组复制到另一个数组,并指定需要拷贝的范围 |
setAll | 设置元素 | setAll(int[] array, IntUnaryOperator generator) setAll(long[] array, IntToLongFunction generator) setAll(double[] array, IntToDoubleFunction generator) setAll(T[] array, IntFunction<? extends T> generator) | 使用提供的生成函数来计算每个元素,来重新设置指定数组的所有元素。 |
parallelSetAll | 并行设置元素 | parallelSetAll(int[] array, IntUnaryOperator generator) parallelSetAll(long[] array, IntToLongFunction generator) parallelSetAll(double[] array, IntToDoubleFunction generator) parallelSetAll(T[] array, IntFunction<? extends T> generator) | 使用提供的生成函数来计算每个元素,来重新设置指定数组的所有元素(并行)。 |
parallelPrefix | 并行计算 | parallelPrefix(int[] array, IntBinaryOperator op) parallelPrefix(int[] array, int fromIndex, int toIndex, IntBinaryOperator op) parallelPrefix(long[] array, LongBinaryOperator op) parallelPrefix(long[] array, int fromIndex, int toIndex, LongBinaryOperator op) parallelPrefix(double[] array, DoubleBinaryOperator op) parallelPrefix(double[] array, int fromIndex, int toIndex, DoubleBinaryOperator op) parallelPrefix(T[] array, BinaryOperator op) parallelPrefix(T[] array, int fromIndex, int toIndex, BinaryOperator op) | 对数组做并行计算。 计算范围:fromIndex(包含),toIndex(不包含) |
spliterator | 分割数组 | spliterator(int[] array) spliterator(int[] array, int startInclusive, int endExclusive) spliterator(long[] array) spliterator(long[] array, int startInclusive, int endExclusive) spliterator(double[] array) spliterator(double[] array, int startInclusive, int endExclusive) spliterator(T[] array) spliterator(T[] array, int startInclusive, int endExclusive) | 分割数组,默认分割后包含数组全部元素。 可以指定分割的范围startInclusive、endExclusive |
stream | 转为流 | stream(int[] array) stream(int[] array, int startInclusive, int endExclusive) stream(long[] array) stream(long[] array, int startInclusive, int endExclusive) stream(double[] array) stream(double[] array, int startInclusive, int endExclusive) stream(T[] array) stream(T[] array, int startInclusive, int endExclusive) | 将数组转为流,可指定包含的元素范围。 |
equals | 比较 | equals(byte[] a, byte[] a2) equals(char[] a, char[] a2) equals(short[] a, short a2[]) equals(int[] a, int[] a2) equals(long[] a, long[] a2) equals(float[] a, float[] a2) equals(double[] a, double[] a2) equals(boolean[] a, boolean[] a2) equals(Object[] a, Object[] a2) | 比较两个数组是否相等 |
deepEquals | 比较 (多维数组) | deepEquals(Object[] a1, Object[] a2) | 比较两个多维数组是否相等 |
hashCode | 哈希 | hashCode(byte a[]) hashCode(char a[]) hashCode(short a[]) hashCode(int a[]) hashCode(long a[]) hashCode(float a[]) hashCode(double a[]) hashCode(boolean a[]) hashCode(Object a[]) | 数组的哈希值 |
deepHashCode | 哈希 (多维数组) | deepHashCode(Object a[]) | 多维数组的哈希值 |
toString | 字符串 | toString(byte[] a) toString(char[] a) toString(short[] a) toString(int[] a) toString(long[] a) toString(float[] a) toString(double[] a) toString(boolean[] a) toString(Object[] a) | 数组转字符串 |
deepToString | 字符串 (多维数组) | deepToString(Object[] a) | 多维数组转字符串 |
方法举例
1. asList
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
2. binarySearch
查找key在数组中的索引位置。如果存在,返回索引位置,否则返回(-(插入点)-1)。
插入点:key将插入数组的索引位置,即第一个大于此key的元素索引位置。如果数组中所有元素都小于key,插入点=数组长度。
特别注意:使用二分查找的数组,数组中的元素必须是按升序排列的,否则会出问题。
int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
System.out.println(Arrays.binarySearch(arr, 1));
System.out.println(Arrays.binarySearch(arr, 20));
System.out.println(Arrays.binarySearch(arr, 1, 2, 99));
结果
0
-11
-3
当数组中的元素未按升序排列时
int[] arr = new int[]{2, 3, 4, 1, 5, 6, 7, 8, 9, 10};
System.out.println(Arrays.binarySearch(arr, 1));
结果
-1
原因分析:当开始与下标0位置的元素2比较时,发现2已经大于1,所有得到插入点为0,得到返回值-1(-0-1)。
3. sort
int[] arr = {1, 2, 3, 7, 6, 5, 4, 8, 9, 10};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
int[] arr1 = {1, 2, 3, 7, 6, 5, 4, 8, 9, 10};
Arrays.sort(arr1, 3, 7);
System.out.println(Arrays.toString(arr1));
Integer[] arr2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Arrays.sort(arr2, (i1,i2) -> i2-i1);
System.out.println(Arrays.toString(arr2));
Integer[] arr3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Arrays.sort(arr3, 0, 5, (i1,i2) -> i2-i1);
System.out.println(Arrays.toString(arr3));
结果
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
[5, 4, 3, 2, 1, 6, 7, 8, 9, 10]
4. parallelSort
int[] arr = {1, 2, 3, 7, 6, 5, 4, 8, 9, 10};
Arrays.parallelSort(arr);
System.out.println(Arrays.toString(arr));
int[] arr1 = {1, 2, 3, 7, 6, 5, 4, 8, 9, 10};
Arrays.parallelSort(arr1, 3, 7);
System.out.println(Arrays.toString(arr1));
Integer[] arr2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Arrays.parallelSort(arr2, (i1,i2) -> i2-i1);
System.out.println(Arrays.toString(arr2));
Integer[] arr3 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Arrays.parallelSort(arr3, 0, 5, (i1,i2) -> i2-i1);
System.out.println(Arrays.toString(arr3));
结果
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
[5, 4, 3, 2, 1, 6, 7, 8, 9, 10]
5. fill
int[] arr1 = {1, 2, 3, 7, 6, 5, 4, 8, 9, 10};
Arrays.fill(arr1, 0);
System.out.println(Arrays.toString(arr1));
int[] arr2 = {1, 2, 3, 7, 6, 5, 4, 8, 9, 10};
Arrays.fill(arr2, 0, 3, 0);
System.out.println(Arrays.toString(arr2));
结果
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 7, 6, 5, 4, 8, 9, 10]
6. copyOf
int[] arr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] arr11 = Arrays.copyOf(arr1, 5);
System.out.println(Arrays.toString(arr11));
int[] arr22 = Arrays.copyOf(arr1, 15);
System.out.println(Arrays.toString(arr22));
结果
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0, 0, 0]
7. copyOfRange
int[] arr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] arr11 = Arrays.copyOfRange(arr1, 0, 5);
System.out.println(Arrays.toString(arr11));
int[] arr22 = Arrays.copyOfRange(arr1, 9, 15);
System.out.println(Arrays.toString(arr22));
结果
[1, 2, 3, 4, 5]
[10, 0, 0, 0, 0, 0]
8. setAll
int[] arr1 = {1, 2, 3, 4, 5};
// i为数组的下标
Arrays.setAll(arr1, i -> arr1[i]*arr1[i]);
System.out.println(Arrays.toString(arr1));
结果
[1, 4, 9, 16, 25]
9. parallelSetAll
int[] arr1 = {1, 2, 3, 4, 5};
// i为数组的下标
Arrays.parallelSetAll(arr1, i -> arr1[i]*arr1[i]);
System.out.println(Arrays.toString(arr1));
结果
[1, 4, 9, 16, 25]
10. parallelPrefix
int[] arr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// 每个下标位置的元素最新值为下标所在位置及的前面所有元素之和。
Arrays.parallelPrefix(arr1, (i1,i2) -> i1+i2);
System.out.println(Arrays.toString(arr1));
int[] arr2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Arrays.parallelPrefix(arr2, 2, 6, (i1,i2) -> i1+i2);
System.out.println(Arrays.toString(arr2));
结果
[1, 3, 6, 10, 15, 21, 28, 36, 45, 55]
[1, 2, 3, 7, 12, 18, 7, 8, 9, 10]
11. spliterator
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Spliterator.OfInt split1 = Arrays.spliterator(arr);
Spliterator.OfInt split2 = split1.trySplit();
split2.forEachRemaining((int a) -> System.out.print(a+ " "));
System.out.println();
split1.forEachRemaining((int a) -> System.out.print(a+ " "));
System.out.println();
结果
1 2 3 4 5
6 7 8 9 10
12. stream
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
IntStream stream = Arrays.stream(arr);
// 求和
System.out.println(stream.sum());
结果
55
13. equals
int[] arr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] arr2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
System.out.println(Arrays.equals(arr1, arr2));
结果
true
14. deepEquals
int[][] arr1 = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}};
int[][] arr2 = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}};
System.out.println(Arrays.deepEquals(arr1,arr2));
结果
true
15. hashCode
int[] arr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
System.out.println(Arrays.hashCode(arr1));
结果
-975991962
16. deepHashCode
int[][] arr1 = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}};
System.out.println(Arrays.deepHashCode(arr1));
结果
979244161
17. toString
int[] arr1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
System.out.println(Arrays.toString(arr1));
结果
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
18. deepToString
int[][] arr1 = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}};
System.out.println(Arrays.deepToString(arr1));
结果
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]