Java Arrays 的相关操作数组排序

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};
        // Arrays.toString(arr) 数组转换为字符串
        String arrTostring = Arrays.toString(arr);
        System.out.println("数组转换为字符串:" + arrTostring);

        // 拷贝数组 Arrays.copyOfRange(arr, 1, 3); 包头不包尾 [1,3)
        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, new Comparator() {
//            @Override
//            public int compare(Student o1, Student o2) {
//            // return o2.age-o1.age;   降序
//                return o1.age-o2.age;
//            }
//        });
        // lambda 表达式
        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;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", height=" + height +
                    ", age=" + age +
                    '}';
        }
    }
}

Java Arrays 的相关操作数组排序_第1张图片

你可能感兴趣的:(Java,java,排序算法,算法)