Arrays类概述,常用功能演示
java.util.Arrays 是一个与数组相关的工具类,里面提供了大量静态方法,用来实现数组常见的操作。
Arrays.toString ( 数组名 ) 把数组 拼接 成一个字符串
将参数数组变成字符串(按照默认格式:[10, 20, 30]),返回数组内容
Arrays.sort ( 数组名 )
Arrays.sort(数组,排序规则)
按照默认升序( 从 小 到 大 )对数组的元素进行排序。
1、如果是数值, sort 默认按照 升序 从 小 到 大,底层使用的是快速排序
2、如果是字符串,sort 默认按照 字母 升序
3、如果是自定义的类型,那么这个自定义的类需要有Comparable或者Comparator接口的支持
Arrays.equals ( 数组名1,数组名2 ) 判断两个数组是否相等 Arrays.fill ( 数组名, 要填充的 值 或 元素 ) 将指定值 填充 到数组之中
- 包头不包尾,包左不包右(左闭右开)
Arrays.binarySearch ( 数组名,要查找的值 ) 二分搜索技术(前提数组必须排好顺序才支持,否则出bug)
对排序后的数组进行 二分法 检索指定的值
返回不存在元素的规律:-(应该插入的位置索引 + 1)
如果要查找的元素是不存在的,返回的是 - 插入点 - 1
为什么要减 1 呢?
- 如果此时,我现在要查找数字0,那么如果返回的值是 - 插入点,此时0是不存在的,但是按照上面的规则 - 插入点,应该就是 - 0
- 为了避免这样的情况,Java在这个基础上又减一
Arrays.copyOf(原数组 新数组长度) 拷贝数组
方法的底层会根据第二个参数来创建新的数组
- 如果 新 数组的长度是 小于 老 数组的长度,会部分拷贝
- 如果 新 数组的长度是 等于 老 数组的长度,会完全拷贝
- 如果 新 数组的长度是 大于 老 数组的长度,会补上默认初始值
Array.copy Of Range(原数据,起始索引,结束索引) 拷贝数组(指定范围) 工具类的设计思想:
- 构造方法用private
- 成员用 public static 修饰
请使用Arrays相关的API,将一个随机字符串中的所有字符 升序排列 ,并 倒叙 打印。
public class Demo02ArraysPractice { public static void main(String[] args) { String str = "asv76agfqwdfvasdfvjh"; char[] chars = str.toCharArray(); Arrays.sort(chars); for (int i = chars.length - 1; i >= 0; i--) { System.out.print(chars[i]); } } } D:\Java\jdk-17\bin\java.exe com.atguigu.array.Demo02ArraysPractice wvvvssqjhgfffddaaa76 Process finished with exit code 0
package com.csdn.pojo; import java.util.Arrays; public class MyArraysDemo1 { public static void main(String[] args) { //toString:将数组变成字符串 int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; System.out.println(Arrays.toString(arr));//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] //binarySearch:二分查找法查找元素 System.out.println(Arrays.binarySearch(arr, 10));//9 System.out.println(Arrays.binarySearch(arr, 2));//1 System.out.println(Arrays.binarySearch(arr, 20));//-11 //copyOf:拷贝数组 int[] newArr1 = Arrays.copyOf(arr, 2); System.out.println(Arrays.toString(newArr1));//[1, 2] int[] newArr2 = Arrays.copyOf(arr, 10); System.out.println(Arrays.toString(newArr2));//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] int[] newArr3 = Arrays.copyOf(arr, 20); System.out.println(Arrays.toString(newArr3));//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] //copyOfRange:拷贝数组(指定范围) int[] newArr4 = Arrays.copyOfRange(arr, 0, 9);//左闭右开 System.out.println(Arrays.toString(newArr4));//[1, 2, 3, 4, 5, 6, 7, 8, 9] //fill:填充数组 Arrays.fill(arr, 100); System.out.println(Arrays.toString(arr));//[100, 100, 100, 100, 100, 100, 100, 100, 100, 100] //sort:排序。默认情况下,给基本数据类型进行升序排列 int[] arr2 = {10, 2, 3, 5, 6, 1, 7, 8, 4, 9}; Arrays.sort(arr2); System.out.println(Arrays.toString(arr2));//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] } }
public class Demo01Arrays { public static void main(String[] args) { int[] intArray = {30,20,10}; String inStr = Arrays.toString(intArray); System.out.println(inStr); Arrays.sort(intArray); System.out.println(Arrays.toString(intArray)); String[] array2 = {"bbb", "aaa", "ccc"}; Arrays.sort(array2); System.out.println(Arrays.toString(array2)); int[] arr1 = new int[]{1, 2, 3, 4}; int[] arr2 = new int[]{1, 2, 3, 4}; boolean isEquals = Arrays.equals(arr1, arr2); System.out.println(isEquals); Arrays.fill(arr1, 10); System.out.println(Arrays.toString(arr1)); int index = Arrays.binarySearch(arr1, 210); System.out.println(index); } }
Arrays类对于Comparator比较器的支持
public static <T> void sort(类型[ ] a,Comparator<? super T >c ) 使用比较器对象自定义排序 自定义排序规则
- 设置Comparator接口对应的比较器对象,来定制比较规则。
- 如果认为左边数据 大于 右边数据返回 正整数
- 如果认为左边数据 小于 右边数据返回 负整数
- 如果认为左边数据 等于 右边数据返回 0
目标:自定义数组的排序规则:Comparator比较器对象
- Arrays的sort方法对于有值特性的数组是默认升序排序
- 需求:降序排列!(自定义比较器对象,只能支持应用类型的排序!!)
参数一:被排序的数组 必须是引用类型的元素
参数二:匿名内部类对象,代表了一个比较器对象
public class ArraysDemo2 { public static void main(String[] args) { int[] ages = {34, 12, 42, 23}; Arrays.sort(ages); System.out.println(Arrays.toString(ages)); Integer[] ages1 = {34, 12, 42, 23}; Arrays.sort(ages1, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { /*if (o1 > o2) { return 1; } else if (o1 < o2) { return -1; } return 0;*/ return o2 - o1; } }); System.out.println(Arrays.toString(ages1)); System.out.println("--------------"); Student[] students = new Student[3]; students[0] = new Student("小红", 23, 175.5); students[1] = new Student("小兰", 18, 185.5); students[2] = new Student("小明", 45, 175.5); System.out.println(Arrays.toString(students)); Arrays.sort(students, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return o1.getAge() - o2.getAge(); } }); System.out.println(Arrays.toString(students)); } }
package com.csdn.pojo; import java.util.Arrays; import java.util.Comparator; public class MyArraysDemo2 { public static void main(String[] args) { Integer[] arr = {2, 3, 1, 5, 6, 7, 8, 4, 9}; //底层原理: /** * 利用 插入排序 + 二分查找的方式进行排序的 * 默认把 0 索引的数组当做是有序的序列,1 索引到最后认为是无序的序列 * 遍历无序的序列得到里面的每一个元素,假设当前遍历得到的元素是 A 元素 * 把 A 往有序序列中进行插入,在插入的时候,是利用二分查找确定 A 元素的插入点 * 拿着 A 元素,跟插入点的元素进行比较,比较的规则就是compare方法的方法体 * 如果方法的返回值是负数,拿着 A 继续跟前面的数据进行比较 * 如果方法的返回值是正数,拿着 A 继续跟后面的数据进行比较 * 如果方法的返回值是 0 ,也拿着 A 跟后面的数据进行比较 * 直到能确定 A 的最终位置为止 */ Arrays.sort(arr, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o1-o2; } }); Arrays.sort(arr, (o1, o2) -> o1 - o2); Arrays.sort(arr, Comparator.comparingInt(o -> o)); System.out.println(Arrays.toString(arr)); } }
用Comparable接口对下列四位同学的 成绩 做降序排序,如果成绩一样,那在成绩排序的基础上按照 年龄 由小到大排序
package com.csdn.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Arrays; @Data @NoArgsConstructor @AllArgsConstructor public class Student implements Comparable<Student> { private String name; private int age; private double score; @Override public int compareTo(Student stu) { if (this.getScore() > stu.getScore()) { return -1; } else if (this.getScore() < stu.getScore()) { return 1; } return this.getAge() - stu.getAge(); } public static void main(String[] args) { Student[] students = new Student[4]; students[0] = new Student("liusan", 20, 90); students[1] = new Student("lisi", 22, 90); students[2] = new Student("wangwu", 20, 99); students[3] = new Student("sunliu", 22, 100); Arrays.sort(students);//自动调用Student中的compareTo()方法 System.out.println(Arrays.toString(students)); /* [Student(name=sunliu, age=22, score=100.0), Student(name=wangwu, age=20, score=99.0), Student(name=liusan, age=20, score=90.0), Student(name=lisi, age=22, score=90.0)] */ } }
用Comparator实现按照 姓名 排序
package com.csdn.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Arrays; import java.util.Comparator; @Data @NoArgsConstructor @AllArgsConstructor public class Student{ private String name; private int age; private double score; public static void main(String[] args) { Student[] students = new Student[4]; students[0] = new Student("d", 20, 90); students[1] = new Student("c", 22, 90); students[2] = new Student("b", 20, 99); students[3] = new Student("a", 22, 100); Arrays.sort(students, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { return o1.getName().compareTo(o2.getName());//此时的compareTo()方法是String中的方法 } }); System.out.println(Arrays.toString(students)); /* [Student(name=a,age=22, score=100.0), Student(name=b, age=20, score=99.0), Student(name=c, age=22, score=90.0), Student(name=d, age=20, score=90.0)] */ } }
声明 泛型 方法,可以接收一个任意引用类型的 数组 ,并 反转 数组中的所有元素
package com.csdn.pojo; public class Test04 { public static <T> void reverse(T[] arr) { for (int i = 0; i < arr.length / 2; i++) { T temp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = temp; } } public static void main(String[] args) { Integer[] arr = {1, 2, 3, 4, 5, 6,}; reverse(arr); for (Integer i : arr) { System.out.print(i+"\t");//6 5 4 3 2 1 } } }
声明 泛型 方法,可以实现任意引用类型 数组 指定位置元素 交换
package com.csdn.pojo; public class Test03 { public static <T> void method(T[] arr, int a, int b) { T temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } public static void main(String[] args) { String[] arr = {"小明", "小红", "小兰", "小姜"}; method(arr,1, 3); for (String s : arr) { System.out.print(s+"\t");//小明 小姜 小兰 小红 } } }
声明 泛型 方法,可以给任意对象 数组 按照元素的自然顺序实现从小到大排序,用 冒泡排序 实现
package com.csdn.pojo; public class Test05 { public static <T extends Comparable<T>> void sort(T[] arr) { for (int i = 1; i < arr.length; i++) { for (int j = 0; j < arr.length - i; j++) { if (arr[j].compareTo(arr[j + 1]) > 0) { T temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } public static void main(String[] args) { Integer[] arr = new Integer[]{9, 3, 5, 1, 5, 8, 3, 2,}; sort(arr); for (Integer i : arr) { System.out.print(i+"\t");//1 2 3 3 5 5 8 9 } } }
声明 泛型 方法,可以给任意对象 数组 按照指定的 比较器 实现从小到大排序,用 冒泡排序 实现
package com.csdn.pojo; import java.util.Comparator; public class Test05 { public static <T> void sort(T[] arr, Comparator<? super T> c) { for (int i = 1; i < arr.length; i++) { for (int j = 0; j < arr.length - i; j++) { if (c.compare(arr[j], arr[j + 1]) > 0) { T temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } public static void main(String[] args) { Integer[] arr = new Integer[]{9, 3, 5, 1, 5, 8, 3, 2,}; // sort(arr, new Comparator<Integer>() { // @Override // public int compare(Integer o1, Integer o2) { // return o1 - o2; // } // }); // sort(arr, (o1, o2) -> o1 - o2); sort(arr, Comparator.comparingInt(o -> o)); for (Integer i : arr) { System.out.print(i+"\t");//1 2 3 3 5 5 8 9 } } }