java基础【使用接口实现通用的排序算法】

package com.hike.javase.interfacetest;

//创建学生类,并实现Comparable接口
public class Student implements Comparable{
    private int id;
    private String name;
    private int grade;
    private double score;

    public Student() {
    }

    public Student(int id, String name, int grade, double score) {
        this.id = id;
        this.name = name;
        this.grade = grade;
        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 int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public double getScore() {
        return score;
    }

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

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


    //想要使用不同的排序策略,只需要改变this.后面的属性就好
    @Override
    public int compareTo(Object o) {
        if(o instanceof Student) {
            Student s = (Student) o;  //对象的造型,造型有风险,提前判断一下
            /*if (this.getGrade() == s.getGrade()) {     //注意:0.getRadius()因为多态的副作用,导致子类的特有方法不可以被调取
                return 0;
            } else if (this.getGrade() > s.getGrade()) {
                return 1;
            } else {
                return -1;
            }*/
            return this.grade - ((Student)o).grade;
        }
        return 0X80000000;
    }
}

package com.hike.javase.interfacetest;

//创建通用的排序方法
public class Sorter {
    public static void sort(Comparable[] arr){  //数组的类型不是Student、String某一具体的类型,只要是可比较的数组,都可以进行比较
        for(int i = 0; i < arr.length - 1; i++){
            for(int j = 0; j < arr.length - 1 - i; j++){
                if(arr[j].compareTo(arr[j + 1]) > 0){
                    Comparable tem = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tem;
                }
            }
        }
    }
}


package com.hike.javase.interfacetest;

public class ComparebleTest {
    public static void main(String[] args) {
        Student s1 = new Student(1, "张三", 4, 90);
        Student s2 = new Student(2, "李四", 5, 66);

        //System.out.println(s1.compareTo(s2));

        Student[] stuArr = new Student[5];
        stuArr[0] = s1;
        stuArr[1] = s2;
        stuArr[2] = new Student(3,"王五",6,99);
        stuArr[3] = new Student(4,"赵六",3,70);
        stuArr[4] = new Student(5,"小微",2,60);

        for(Student s : stuArr){
            System.out.println(s.toString());
        }

        System.out.println("****************************************");

        Sorter.sort(stuArr);
        for(Student s : stuArr){
            System.out.println(s.toString());
        }

        String[] stringArr = {"aa","zz","bbb","aaa"};
        Sorter.sort(stringArr);
        for(String s : stringArr){
            System.out.println(s);
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

OneTenTwo76

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值