Arrays
- 用来操作数组的一个工具类
Arrays提供的常见方法
方法名 | 说明 |
---|---|
public static String toString(类型 [ ] arr) | 返回数组的内容 |
public static int [ ] copyOfRange(类型 [ ] arr,起始索引,结束索引) | 拷贝数组(指定范围) |
public static copyOf(类型 [ ] arr,int newLength) | 拷贝数组 |
public static setAll(double [ ] array,IntToDoubleFunction generator) | 把数组中的原数据改为新数据 |
public static void sort(类型 [ ] arr) | 对数组进行排序(默认是升序排序) |
import java.util.Arrays;
import java.util.function.IntToDoubleFunction;public class Test {public static void main(String[] args) {// 返回数组内容int[] arr = {10,20,30,40,50,60};System.out.println(Arrays.toString(arr));// 拷贝数组,指定范围(包前不包后)int[] arr2 = Arrays.copyOfRange(arr,1,4);System.out.println(arr2);// 拷贝数组,可以指定新数组的长度int[] arr3 = Arrays.copyOf(arr,10);System.out.println(arr3);// 把数组中的新数据改为新数组又存进去double[] prices = {99.8,128,100};// 例如:把价格都打八折再存进去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(比较规则)接口,然后重写compareTo方法,自己来制定比较规则。
Student类
public class Student implements Comparable<Student>{private String name;private double height;private int age;@Overridepublic int compareTo(Student o) {// 约定1:认为左边对象 大于 右边对象 返回正整数// 约定2:认为左边对象 小于 右边对象 返回负整数// 约定3:认为左边对象 等于 右边对象 一定返回0// 按年龄升序/** if(this.age > o.age){* return 1;* }else if(this.age < o.age){* return -1;* }* return 0;* */return this.age - o.age;//return o.age - this.age; 降序}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", height=" + height +", age=" + age +'}';}public Student() {}public Student(String name, double height, int age) {this.name = name;this.height = height;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
Test类
import java.util.Arrays;public class Test {public static void main(String[] args) {// 目标:掌握如何对数组中的对象进行排序Student[] students = new Student[4];students[0] = new Student("张三",175.6,23);students[1] = new Student("李四",169,20);students[2] = new Student("王五",180.3,18);students[3] = new Student("赵六",165.9,25);// 对数组进行排序Arrays.sort(students);System.out.println(Arrays.toString(students));}
}
方法二:使用下面者给sort方法,创建Compareato比较器接口的匿名内部类对象,然后自己制定比较规则。
import java.util.Arrays;
import java.util.Comparator;public class Test {public static void main(String[] args) {// 目标:掌握如何对数组中的对象进行排序Student[] students = new Student[4];students[0] = new Student("张三",175.6,23);students[1] = new Student("李四",169,20);students[2] = new Student("王五",180.3,18);students[3] = new Student("赵六",165.9,25);// 对数组进行排序Arrays.sort(students, new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {// 约定1:认为左边对象 大于 右边对象 返回正整数// 约定2:认为左边对象 小于 右边对象 返回负整数// 约定3:认为左边对象 等于 右边对象 一定返回0// 按年龄升序/** if(o1.getHeight() > o2.getHeight()){* return 1;* }else if(o1.getHeight() < o2.getHeight()){* return -1;* }* return 0;* */return Double.compare(o1.getHeight(),o2.getHeight()); //升序//return Double.compare(o1.getHeight(),o2.getHeight()); 降序}});System.out.println(Arrays.toString(students));}
}