ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] # Arrays 这个工具类可以完成所有与数组有关的操作功能 ## asList方法,返回一个固定大小的List ~~~ @SafeVarargs public static <T> List<T> asList(T... a) { return new ArrayList<>(a); } ~~~ 使用该方法可以返回一个固定大小的List,如: ~~~ List<String> stringList = Arrays.asList("Welcome", "To", "Java", "World!"); List<Integer> intList = Arrays.asList(1, 2, 3, 4); ~~~ ## binarySearch方法 **需要排好序的** binarySearch方法支持在整个数组中查找,如: ~~~ int index = Arrays.binarySearch(new int[] { 1, 2, 3, 4, 5, 6, 7 }, 6); ~~~ 以及在某个区间范围内查找, 如: ~~~ public static int binarySearch(int[] a, int fromIndex, int toIndex, int key) { rangeCheck(a.length, fromIndex, toIndex); return binarySearch0(a, fromIndex, toIndex, key); } int index = Arrays.binarySearch(new int[] { 1, 2, 3, 4, 5, 6, 7 }, 1, 6, 6); ~~~ ## copyOf及copyOfRange方法,复制数组 注意:如果复制的是对象数组,那么只是对象的引用,而不是对象本身的拷贝 ~~~ String[] names2 = { "Eric", "John", "Alan", "Liz" }; //[Eric, John, Alan] String[] copy = Arrays.copyOf(names2, 3); //[Alan, Liz] String[] rangeCopy = Arrays.copyOfRange(names2, 2, names2.length); ~~~ ## sort方法 ~~~ String[] names = { "Liz", "John", "Eric", "Alan" }; //只排序前两个 //[John, Liz, Eric, Alan] Arrays.sort(names, 0, 2); //全部排序 //[Alan, Eric, John, Liz] Arrays.sort(names); ~~~ 另外,Arrays的sort方法也可以结合比较器,完成更加复杂的排序。 ~~~ public static <T> void sort(T[] a, Comparator<? super T> c) { if (LegacyMergeSort.userRequested) legacyMergeSort(a, c); else TimSort.sort(a, c); } ~~~ ## toString方法,方便打印出数组内容 Arrays的toString方法可以方便我们打印出数组内容。 如: ~~~ String[] names = { "Liz", "John", "Eric", "Alan" }; Arrays.sort(names); System.out.println(Arrays.toString(names)); ~~~ 控制台将打印出`[Alan, Eric, John, Liz] ` ## deepToString方法,打印二维数组的内容 如果需要打印二维数组的内容: ~~~ int[][] stuGrades = { { 80, 81, 82 }, { 84, 85, 86 }, { 87, 88, 89 } }; ~~~ 如果直接用 ~~~ System.out.println(Arrays.toString(stuGrades)); ~~~ 那么得到的结果类似于 ~~~ [[I@35ce36, [I@757aef, [I@d9f9c3]} ~~~ 这个时候得用 deepToString 方法才能得到正确的结果`[[80, 81, 82], [84, 85, 86], [87, 88, 89]]` ~~~ System.out.println(Arrays.deepToString(stuGrades)); ~~~ ## equals方法 使用Arrays.equals来比较1维数组是否相等。 **需要数组顺序完全一致** ~~~ String[] names1 = { "Eric", "John", "Alan", "Liz" }; String[] names2 = { "Eric", "John", "Alan", "Liz" }; System.out.println(Arrays.equals(names1, names2)); ~~~ ## deepEquals方法,断更加复杂的数组是否相等 Arrays.deepEquals能够去判断更加复杂的数组是否相等。 ~~~ int[][] stuGrades1 = { { 80, 81, 82 }, { 84, 85, 86 }, { 87, 88, 89 } }; int[][] stuGrades2 = { { 80, 81, 82 }, { 84, 85, 86 }, { 87, 88, 89 } }; System.out.println(Arrays.deepEquals(stuGrades1, stuGrades2)); ~~~ ## fill方法,填充数组 将指定的元素放在指定的数组中 ~~~ int[] array1 = new int[8]; Arrays.fill(array1, 1); //[1, 1, 1, 1, 1, 1, 1, 1] System.out.printl ~~~ ## parallelsort并行排序 ~~~ double[] numbers = {6.0, 4.4, 1.9, 2.9, 3.4, 3.5}; Arrays.parallelSort(numbers);//对整个数组进行排序; Arrays.parallelSort(numbers); char[] chars = {'a', 'A', '4', 'F', 'D', 'P'}; Arrays.parallelSort(chars, 1, 3);//对部分数组进行排序,从chars[1]到chars[3]; Arrays.sort(chars, 1, 3); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i]); } ~~~