Java Arrays 的相关操作数组排序
package com.zhong.arrays;import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Comparator;public class ArraysDemo {public static void main(String[] args) {int[] arr = {10, 20, 40, 30, 90, 60, 10, 30, 50};String arrTostring = Arrays.toString(arr);System.out.println("数组转换为字符串:" + arrTostring);int[] copyOfRangeArray = Arrays.copyOfRange(arr, 1, 3);System.out.println("拷贝数组:" + Arrays.toString(copyOfRangeArray));int[] copyOfArrayLength = Arrays.copyOf(arr, 5);System.out.println(Arrays.toString(copyOfArrayLength));double[] oldPrice = {99, 60, 40, 34};Arrays.setAll(oldPrice, value -> Double.parseDouble(String.valueOf(BigDecimal.valueOf(oldPrice[value]).multiply(BigDecimal.valueOf(0.7)))));Arrays.setAll(oldPrice, value -> value * 0.7); System.out.println(Arrays.toString(oldPrice));Arrays.sort(arr);System.out.println(Arrays.toString(arr));Student[] student = new Student[5];student[0] = new Student("小钟", 179.1, 22);student[1] = new Student("小王", 152.1, 21);student[2] = new Student("小张", 199.2, 32);student[3] = new Student("小红", 120.8, 92);student[4] = new Student("小芳", 169.3, 54);Student[] students = sortObject(student);for (Student student1 : students) {System.out.println(student1);}}private static Student[] sortObject(Student[] student) {
Arrays.sort(student, (o1, o2) -> o2.age - o1.age );return student;}static class Student {private String name;private Double height;private int 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;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", height=" + height +", age=" + age +'}';}}
}