实现数组升序,降序,乱序(数字数组,字符串数组,对象数组)

实现数组升序,降序,乱序(数字数组,字符串数组,对象数组,字符串长度)

//数字数组
        Integer[] i = new Integer[]{5, 4, 6, 8, 96, 51};
        //升序
        Arrays.sort(i);
        System.out.println(Arrays.toString(i));
        //降序
        Arrays.sort(i, (a, b) -> b - a);
        System.out.println(Arrays.toString(i));
        //乱序
        Arrays.sort(i, (a, b) -> Math.random() > .5 ? 1 : -1);
        System.out.println(Arrays.toString(i));

        //字符串数组
        String[] y = {"中国", "java", "c++", "A股", "Bilibili"};
        //升序
        Arrays.sort(y);
        System.out.println(Arrays.toString(y));
        //降序
        Arrays.sort(y, (a, b) -> b.compareTo(a));
        System.out.println(Arrays.toString(y));
        //乱序
        Arrays.sort(y, (a, b) -> Math.random() > .5 ? 1 : -1);
        System.out.println(Arrays.toString(y));

        //对象数组
        Student[] student = new Student[]{new Student(10, "王一", LocalDate.of(2001, 12, 13), 95), new Student(5, "李二", LocalDate.of(2005, 12, 13), 96), new Student(6, "张三", LocalDate.of(2008, 12, 13), 98), new Student(15, "范五", LocalDate.of(2007, 12, 13), 99), new Student(14, "葛四", LocalDate.of(2002, 12, 13), 91)};
        //升序
        Arrays.sort(student, (a, b) -> a.getId() - b.getId());
        System.out.println(Arrays.toString(student) + "-".repeat(5));
        //乱序
        Arrays.sort(student, (a, b) -> Math.random() > .5 ? 1 : -1);
        System.out.println(Arrays.toString(student) + "-".repeat(5));
        //降序
        Arrays.sort(student, (a, b) -> b.getBir().compareTo(a.getBir()));
        System.out.println(Arrays.toString(student) + "-".repeat(5));
        //升序
        Arrays.sort(student, (a, b) -> b.getScore() - a.getScore());
        System.out.println(Arrays.toString(student) + "-".repeat(5));

在这里插入图片描述
对象数组

public class Student implements Comparable<Student> {
    private int id;
    private String name;
    private LocalDate bir;
    private int score;

    @Override
    public String toString() {
        return "Student{" + "id=" + id + ", name='" + name + '\'' + ", bir=" + bir + ", score=" + score + '}';
    }

    public Student() {
    }

    public Student(int id, String name, LocalDate bir, int score) {
        this.id = id;
        this.name = name;
        this.bir = bir;
        this.score = score;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public LocalDate getBir() {
        return bir;
    }

    public void setBir(LocalDate bir) {
        this.bir = bir;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    @Override
    public int compareTo(Student o) {
        return 0;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值