Arrays所在的包是Java.util.*,Arrays提供的全部是static方法。
1.转字符串
1.1 一维数组--public static String toString(int[] a)
参数即可以是基础类型数组,也可以是包装类型数组,也可以是对象数组。
// 一维数组转字符串
int[] arrays = new int[]{1,2,3,4,5};
System.out.println(Arrays.toString(arrays)); // [1,2,3,4,5]
1.2.二维数组--public static String deepToString(Object[] a)
// 二维数组转字符串
int[][] arrays2 = new int[][]{{1,2,3,4,5}, {6,7,8,9,10}};
System.out.println(Arrays.deepToString(arrays2)); // [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]
2.转列表
2.1 一维数组--public static List asList(T... a)
2.1.1 泛型参数
// 一维数组转转列表(泛型参数:基础类型数组要转为包装类型数组)
int[] arrays3 = new int[]{1,2,3,4,5};
System.out.println(Arrays.asList(arrays3)); // 对象(基础类型无法泛型化,对象可以泛型化)
Integer[] arrays4 = new Integer[]{1,2,3,4,5};
System.out.println(Arrays.asList(arrays4)); // [1,2,3,4,5]
2.1.2 改变列表长度
// 一维数组转转列表(改变列表长度:会抛异常,通过new ArrayList规避)
Integer[] arrays5 = new Integer[]{1,2,3,4,5};
List<Integer> list = Arrays.asList(arrays5);
// list.add(6); // 抛异常(无法修改长度)
// list.remove(); // 抛异常(无法修改长度)
List<Integer> list2 = new ArrayList<>(Arrays.asList(arrays5)); // 修改长度的方法
list2.add(6);
System.out.println(list2); // [1,2,3,4,5,6]
2.1.3 改变列表内容
// 一维数组转转列表(改变列表内容:数组同时变化)
Integer[] arrays6 = new Integer[]{1,5,2,3,4};
List<Integer> list3 = new ArrayList<>(Arrays.asList(arrays6));
list3.sort(Comparator.naturalOrder());
System.out.println(list3); // [1,2,3,4,5,6]
System.out.println(arrays6); // [1, 2, 3, 4, 5]
2.2 二维数组--public static List asList(T... a)
// 二维数组转列表
Integer[][] arrays7 = new Integer[][]{{1,2,3,4,5}, {6,7,8,9,10}};
List<Integer[]> list4 = Arrays.asList(arrays7);
System.out.println(list4);
3.相等
3.1 一维数组--public static boolean equals(int[] a, int[] a2)
// 一维数组判断是否相等:顺序和值都相同
int[] arrays = new int[]{1,2,3,4,5};
int[] arrays2 = new int[]{1,2,3,5,4};
int[] arrays3 = new int[]{1,2,3,4,5};
System.out.println(Arrays.equals(arrays,arrays2)); // false
System.out.println(Arrays.equals(arrays,arrays3)); // true
3.2 二维数组--public static boolean deepEquals(Object[] a1, Object[] a2)
// 二维数组判断是否相等:顺序和值都相同
Integer[][] arrays4 = new Integer[][]{{1,2,3,4,5}, {6,7,8,9,10}};
Integer[][] arrays5 = new Integer[][]{{1,2,3,5,4}, {6,7,8,9,10}};
Integer[][] arrays6 = new Integer[][]{{1,2,3,4,5}, {6,7,8,9,10}};
System.out.println(Arrays.equals(arrays4,arrays5)); // false
System.out.println(Arrays.deepEquals(arrays4,arrays5)); // false
System.out.println(Arrays.deepEquals(arrays4,arrays6)); // true
4.填充
4.1 一维数组--public static void fill(boolean[] a, boolean val)
4.1.1 给空数组填充
// 给空数组填充,还是空数组
int[] arrays = new int[]{};
Arrays.fill(arrays, 1);
System.out.println(Arrays.toString(arrays)); // []
4.1.2 给非空数组填充(全部填充/部分填充)
// 全部填充
Integer[] arrays2 = new Integer[]{1,2,3,4,5};
Arrays.fill(arrays2, 1);
System.out.println(Arrays.toString(arrays2)); // [1, 1, 1, 1, 1]
// 部分填充
int[] arrays3 = new int[]{1,2,3,4,5};
Arrays.fill(arrays3, 1,3,8);
System.out.println(Arrays.toString(arrays3)); // [1, 8, 8, 4, 5]
4.2 二维数组--不支持
5.复制
5.1 一维数组
5.1.1 copyOf
// 拷贝--copyOf
Integer[] array1 = new Integer[]{1,2,3,4,5};
Integer[] array2 = Arrays.copyOf(array1, 3);
System.out.println(Arrays.toString(array2)); // [1, 2, 3]
5.1.2 copyOfRange
// 拷贝--copyOfRange 左闭右开
Integer[] array3 = Arrays.copyOfRange(array1, 3,5);
System.out.println(Arrays.toString(array3)); // [4, 5]
5.2 二维数组--不支持
6.查找
6.1 一维数组
public static int binarySearch(int[] a, int key)
public static int binarySearch(int[] a, int fromIndex, int toIndex,int key)
public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c)
public static <T> int binarySearch(T[] a, int fromIndex, int toIndex,T key, Comparator<? super T> c)
6.1.1 二分查找
规则:
// 数组无序
Integer[] array = new Integer[]{1,2,3,8,5,4,4};
Integer index1 = Arrays.binarySearch(array, 5); // -4
System.out.println(index1);
// 数组有序
Arrays.sort(array);
Integer index2 = Arrays.binarySearch(array, 5);
System.out.println(index2); // 5
// 多个相同元素
Integer index3 = Arrays.binarySearch(array, 4);
System.out.println(index3); // 3
// 不包含该元素
Integer index4 = Arrays.binarySearch(array, 7);
System.out.println(index4); // -7
6.1.2 比较器查找
ComparableTest[] comparableTests = new ComparableTest[]{
new ComparableTest("林青霞", 27),
new ComparableTest("风清扬", 30),
new ComparableTest("刘晓曲", 28)
};
int index=Arrays.binarySearch(comparableTests, new ComparableTest("林青霞", 27));
System.out.println(index); // 0
public class ComparableTest implements Comparable<ComparableTest> {
private String name;
private int age;
public ComparableTest(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int compareTo(ComparableTest s) {
int num = this.age - s.age;
int num1 = (num == 0 ? this.name.compareTo(s.name) : num);
return num1;
}
}
6.2 二维数组--不支持
7.排序
7.1 一维数组
7.1.1 默认排序(升序)
// 全部排序
Integer[] array = new Integer[]{1,2,3,8,5,4,4};
Arrays.sort(array); // 默认升序排列
System.out.println(Arrays.toString(array)); // [1, 2, 3, 4, 4, 5, 8]
// 部分排序--左闭右开
Integer[] array2 = new Integer[]{1,2,3,8,5,4,4};
Arrays.sort(array2, 0,5); // 默认升序排列
System.out.println(Arrays.toString(array2)); // [1, 2, 3, 5, 8, 4, 4]
7.1.2 比较器排序
全部排序和部分排序
// 全部排序
ComparableTest[] comparableTests = new ComparableTest[]{
new ComparableTest("林青霞", 31),
new ComparableTest("刘晓曲", 30),
new ComparableTest("风清扬", 28)
};
Arrays.sort(comparableTests);
for (int index = 0; index < comparableTests.length; index++) {
System.out.println(comparableTests[index].getName());
}
// 全部排序
Integer[] array = new Integer[]{1,2,3,8,5,4,4};
Arrays.sort(array, new ComparatorTest());
System.out.println(Arrays.toString(array));
// 部分排序
ComparableTest[] comparableTests2 = new ComparableTest[]{
new ComparableTest("林青霞", 31),
new ComparableTest("刘晓曲", 30),
new ComparableTest("风清扬", 28)
};
Arrays.sort(comparableTests2, 0, 2);
for (int index = 0; index < comparableTests2.length; index++) {
System.out.println(comparableTests2[index].getName());
}
// 全部排序
Integer[] array2 = new Integer[]{1,2,3,8,5,4,4};
Arrays.sort(array2, 0,4, new ComparatorTest());
System.out.println(Arrays.toString(array2)); // [8, 3, 2, 1, 5, 4, 4]
7.2 二维数组--不支持
8.计算
8.1 一维数组
8.1.1 单个元素--setAll/parallelSetAll
int[] array = {1, 2, 3, 4, 5};
Arrays.setAll(array,x->x*x);
System.out.println(Arrays.toString(array)); // [0, 1, 4, 9, 16]
int[] array2 = {1, 2, 3, 4, 5};
Arrays.parallelSetAll(array2,x->x*x);
System.out.println(Arrays.toString(array2)); // [0, 1, 4, 9, 16]
8.1.2 两个元素--parallelPrefix
String[] s1 = {"java", "c++", "html", "css", "Javascript", "world"};
System.out.println(Arrays.toString(s1)); //[java, c++, html, css, Javascript, world]
Arrays.parallelPrefix(s1, String::concat);
System.out.println(Arrays.toString(s1)); //[java, javac++, javac++html, javac++htmlcss, javac++htmlcssJavascript, javac++htmlcssJavascriptworld]
int[] a = {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(a)); //[1, 2, 3, 4, 5]
Arrays.parallelPrefix(a, (x, y) -> x * y);
System.out.println(Arrays.toString(a)); //[1, 2, 6, 24, 120]
Arrays.parallelPrefix(a, 2, 4, (x, y) -> x / y);
System.out.println(Arrays.toString(a)); //[1, 2, 6, 0, 120] 也可指定范围