Arrays:
用来操作数组的工具类。
解释说明:
只要知道代码这么写就可以了。
package cn.ensource.d5_arrays;import java.util.Arrays;
import java.util.function.IntToDoubleFunction;public class ArraysTest1 {public static void main(String[] args) {//int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };System.out.println(Arrays.toString(arr));int[] arr2 = Arrays.copyOfRange(arr, 1, 4); // 包前不包后System.out.println(Arrays.toString(arr2));int[] arr3 = Arrays.copyOf(arr, 20); // 对数组进行扩容System.out.println(Arrays.toString(arr3));double[] prices = {99.8, 128, 100.0};System.out.println(Arrays.toString(prices));// 把所有的数据打八折,然后再存进去// 匿名内部类Arrays.setAll(prices, new IntToDoubleFunction() {@Overridepublic double applyAsDouble(int value) {return prices[value] * 0.8;}});System.out.println(Arrays.toString(prices));// 对数组进行排序Arrays.sort(prices);System.out.println(Arrays.toString(prices));}
}
如果数组中存储的是对象,如何排序?
方法一:让该对象的类实现Comparable(比较规则)接口,然后重写Comparable方法,自己来制定比较细则。
方法二:下面这个sort方法,创建Comparator比较接口的匿名内部类对象,然后自己制定比较规则。
Student类:
package cn.ensource.d5_arrays;public class Student implements Comparable<Student>{private String name;private int age;private double height;public Student() {}public Student(String name, int age, double height) {this.name = name;this.age = age;this.height = height;}// 指定比较规则// this和student两个对象@Overridepublic int compareTo(Student student) {// 约定:如果左边对象大于右边对象,请您返回正整数// 如果左边对象小于右边对象,返回负整数// 如果左边对象等于右边对象,请您一定返回0// 按照年龄升序排列
// if(this.age > student.age){
// return 1;
// }else if(this.age < student.age){
// return -1;
// }
// return 0;return this.age - student.age; // 升序}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", height=" + height +'}';}
}
测试类:
package cn.ensource.d5_arrays;import java.util.Arrays;public class ArrayTest2 {public static void main(String[] args) {// 目标:掌握如何对数组中的对象进行排序Student[] students = new Student[4];students[0] = new Student("蜘蛛精", 18, 169.5);students[1] = new Student("狐狸精", 22, 163.8);students[2] = new Student("紫霞", 33, 163.8);students[3] = new Student("至尊宝", 44, 167.5);// sort(类型[] arr] 对数组进行排序Arrays.sort(students);// 为排序对象自定义排序规则System.out.println(Arrays.toString(students));}
}
方式二:
package cn.ensource.d5_arrays;import java.util.Arrays;
import java.util.Comparator;public class ArrayTest2 {public static void main(String[] args) {// 目标:掌握如何对数组中的对象进行排序Student[] students = new Student[4];students[0] = new Student("蜘蛛精", 18, 169.5);students[1] = new Student("狐狸精", 22, 163.8);students[2] = new Student("紫霞", 33, 163.8);students[3] = new Student("至尊宝", 44, 167.5);// sort(类型[] arr] 对数组进行排序
// Arrays.sort(students);// 为排序对象自定义排序规则
// System.out.println(Arrays.toString(students));// public static <T> void sort(T[] arr, Comparator<? super T> c)// 参数一:需要排序的数组// 参数二:Comparator比较器对象,c用来制定对象的比较规则Arrays.sort(students, new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {// 制定比较规则,左边对象是o1, 右边对象是o2
// if(o1.getHeight() > o2.getHeight()) {
// return 1;
// }else if(o1.getHeight() < o2.getHeight()) {
// return -1;
// }
// return 0;return Double.compare(o1.getHeight(), o2.getHeight());}});System.out.println(Arrays.toString(students));}
}