方法名 | 说明 |
---|---|
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() {
@Override
public double applyAsDouble(int value) {
return prices[value] * 0.8;
}
});
System.out.println(Arrays.toString(prices));
// 对数组进行排序(默认是升序)
Arrays.sort(prices);
System.out.println(Arrays.toString(prices));
}
}
Student类?
public class Student implements Comparable<Student>{
private String name;
private double height;
private int age;
@Override
public 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; 降序
}
@Override
public 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));
}
}
?
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>() {
@Override
public 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));
}
}