Arrays类
Arrays类常用API
1 public class array_demo1 { 2 public static void main(String[] args) { 3 int[] arr = {7, 27 ,3 ,12 ,5}; 4 System.out.println(arr); 5 6 // 1.返回数组内容 toString(数组), 遍历 7 System.out.println(Arrays.toString(arr)); 8 9 //2. 排序API(默认升序) 10 Arrays.sort(arr); 11 System.out.println(Arrays.toString(arr)); 12 13 // 3.二分搜索技术(前提数组必须排好序,否则有可能会找不到这个数据的索引) 14 //返回索引 15 int index = Arrays.binarySearch(arr, 12); 16 System.out.println(index); 17 18 // 返回不存在元素的规律: -(应该插入的位置索引+1) 19 int index1 = Arrays.binarySearch(arr, 15); 20 System.out.println(index1); 21 22 } 23 }
Comparator
自定义排序规则: 设置Comparator接口对应的比较器对象,来制定比较规则。
如果认为左边数据大于右边数据,返回正整数。
如果认为左边数据小于右边数据,返回负整数。
如果认为左边数据等于右边数据,返回0。
返回整数是升序,负数是降序。
1 import java.util.Arrays; 2 import java.util.Comparator; 3 4 /** 5 * 自动以数组的排序规则,Comparator比较器对象 6 */ 7 public class array_demo2 { 8 public static void main(String[] args) { 9 // 1.Arrays的sort方法对于有值特性的数组是默认升序排序 10 int[] ages = {22, 33, 11, 32}; 11 Arrays.sort(ages); 12 System.out.println(Arrays.toString(ages)); 13 14 // 降序排序 (自定义比较器对象,只能支持引用类型的排序) 15 Integer[] ages1 = {34, 21, 45, 12}; 16 Arrays.sort(ages1, new Comparator<Integer>() { 17 @Override 18 public int compare(Integer o1, Integer o2) { 19 20 //降序 21 return -(o1 - o2); 22 } 23 }); 24 System.out.println(Arrays.toString(ages1)); 25 26 27 //学生 28 System.out.println("----------------------------------"); 29 30 array_student[] as = new array_student[3]; 31 as[0] = new array_student("wl", 22, 170); 32 as[1] = new array_student("xh", 23, 160); 33 as[2] = new array_student("xx", 21, 172); 34 System.out.println(Arrays.toString(as)); 35 36 Arrays.sort(as, new Comparator<array_student>() { 37 @Override 38 public int compare(array_student o1, array_student o2) { 39 //自己制定规则 40 //按照年龄升序 41 return o1.getAge() - o2.getAge(); 42 //比较浮点型可以这样写 43 //return Double.compare(o1.getHeight(), o1.getHeight()); 44 } 45 }); 46 System.out.println(Arrays.toString(as)); 47 48 } 49 }
1 import java.util.Arrays; 2 import java.util.Comparator; 3 4 /** 5 * 自动以数组的排序规则,Comparator比较器对象 6 */ 7 public class array_demo2 { 8 public static void main(String[] args) { 9 // 1.Arrays的sort方法对于有值特性的数组是默认升序排序 10 int[] ages = {22, 33, 11, 32}; 11 Arrays.sort(ages); 12 System.out.println(Arrays.toString(ages)); 13 14 // 降序排序 (自定义比较器对象,只能支持引用类型的排序) 15 Integer[] ages1 = {34, 21, 45, 12}; 16 Arrays.sort(ages1, new Comparator<Integer>() { 17 @Override 18 public int compare(Integer o1, Integer o2) { 19 20 //降序 21 return -(o1 - o2); 22 } 23 }); 24 System.out.println(Arrays.toString(ages1)); 25 26 27 //学生 28 System.out.println("----------------------------------"); 29 30 array_student[] as = new array_student[3]; 31 as[0] = new array_student("wl", 22, 170); 32 as[1] = new array_student("xh", 23, 160); 33 as[2] = new array_student("xx", 21, 172); 34 System.out.println(Arrays.toString(as)); 35 36 Arrays.sort(as, new Comparator<array_student>() { 37 @Override 38 public int compare(array_student o1, array_student o2) { 39 //自己制定规则 40 //按照年龄升序 41 return o1.getAge() - o2.getAge(); 42 //比较浮点型可以这样写 43 //return Double.compare(o1.getHeight(), o1.getHeight()); 44 } 45 }); 46 System.out.println(Arrays.toString(as)); 47 48 } 49 }
1 public class array_student { 2 private String name; 3 private int age; 4 private double height; 5 6 public array_student() { 7 } 8 9 public array_student(String name, int age, double height) { 10 this.name = name; 11 this.age = age; 12 this.height = height; 13 } 14 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 public int getAge() { 24 return age; 25 } 26 27 public void setAge(int age) { 28 this.age = age; 29 } 30 31 public double getHeight() { 32 return height; 33 } 34 35 public void setHeight(double height) { 36 this.height = height; 37 } 38 39 @Override 40 public String toString() { 41 return "array_student{" + 42 "name='" + name + '\'' + 43 ", age=" + age + 44 ", height=" + height + 45 '}'; 46 } 47 }