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) | 对数组进行排序,默认是升序排序 |
案例演示
public class ArraysTest {
public static void main(String[] args) {
//public static String toString(类型[] arr):返回数组的内容
int[] IntArr = new int[] {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(IntArr)); //[10, 20, 30, 40, 50]
//public static int[] copyOfRange(类型[] arr, 起始索引, 结束索引):拷贝数组(指定范围,包前不包后)
System.out.println("-------------");
int[] IntArr2 = Arrays.copyOfRange(IntArr, 1, 4);
System.out.println(Arrays.toString(IntArr2)); //[20, 30, 40]
//public static copyOf(类型[] arr, int newLength):拷贝数组,可以指定新数组的长度
System.out.println("-----------------");
int[] IntArr3 = Arrays.copyOf(IntArr, 10);
System.out.println(Arrays.toString(IntArr3)); //[10, 20, 30, 40, 50, 0, 0, 0, 0, 0]
System.out.println(IntArr3.length); //10
//public static setAll(double[] array, IntToDoubleFunction generator):把数组中的原数据改为新数据又存进去
System.out.println("--------------------");
double[] numbers = new double[] {12.12, 55.6, 33.4, 100};
//将数组中所有元素乘上100后又存进去
Arrays.setAll(numbers, new IntToDoubleFunction() {
@Override
public double applyAsDouble(int value) {
return numbers[value] * 100;
}
});
System.out.println(Arrays.toString(numbers)); //[1212.0, 5560.0, 3340.0, 10000.0]
//public static void sort(类型[] arr):对数组进行排序(默认是升序排序)
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers)); //[1212.0, 3340.0, 5560.0, 10000.0]
}
}
对于数组中存放对象的情况下,有两种排序方式:
一、让该对象的类实现Comparable(比较规则)接口,然后重写compareTo()方法,自己来制定比较规则
使用以下sort方法,创建Comparator比较器接口的匿名内部类对象,然后自己指定比较规则:
public static void sort(T[] arr, Comparator<? super T> C) 对数组进行排序(支持自定义排序规则)
案例演示
Student类
public class Student implements Comparable<Student> {
private String name;
private int age;
private double score;
public Student() {
}
public Student(String name, int age, double score) {
this.name = name;
this.age = age;
this.score = score;
}
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 getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "{" +
name +
", age=" + age +
", score=" + score +
'}';
}
@Override
public int compareTo(Student student) {
//左边对象大于右边(this.xx > student.xx),返回正整数
//左边对象小于右边(this.xx < student.xx),返回负整数
//左边对象等于右边(this.xx == student.xx),返回0
if (this.score > student.score)
return 1;
else if (this.score < student.score)
return -1;
return 0;
}
}
测试类ArraysTest2
public class ArraysTest2 {
public static void main(String[] args) {
Student[] students = new Student[4];
students[0] = new Student("王亮", 17, 85.5);
students[1] = new Student("李雷", 16, 95);
students[2] = new Student("韩梅梅", 16, 96);
students[3] = new Student("李华", 18, 73);
//类实现Comparable接口并重写compareTo()方法
// Arrays.sort(students);
// System.out.println(Arrays.toString(students));
//Comparator比较器对象(用来制定对象的比较规则)
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));
}
}