吴裕雄--天生自然java开发常用类库学习笔记:比较器

class Student implements Comparable<Student> {    // 指定类型为Student
    private String name ;
    private int age ;
    private float score ;
    public Student(String name,int age,float score){
        this.name = name ;
        this.age = age ;
        this.score = score ;
    }
    public String toString(){
        return name + "\t\t" + this.age + "\t\t" + this.score ;
    }
    public int compareTo(Student stu){    // 覆写compareTo()方法,实现排序规则的应用
        if(this.score>stu.score){
            return -1 ;
        }else if(this.score<stu.score){
            return 1 ;
        }else{
            if(this.age>stu.age){
                return 1 ;
            }else if(this.age<stu.age){
                return -1 ;
            }else{
                return 0 ;
            }
        }    
    }
};
public class ComparableDemo01{
    public static void main(String args[]){
        Student stu[] = {new Student("张三",20,90.0f),
            new Student("李四",22,90.0f),new Student("王五",20,99.0f),
            new Student("赵六",20,70.0f),new Student("孙七",22,100.0f)} ;
        java.util.Arrays.sort(stu) ;    // 进行排序操作
        for(int i=0;i<stu.length;i++){    // 循环输出数组中的内容
            System.out.println(stu[i]) ;
        }
    }
};
public class ComparableDemo02{
    public static void main(String args[]){
        Comparable com = null ;            // 声明一个Comparable接口对象
        com = 30 ;                        // 通过Integer为Comparable实例化
        System.out.println("内容为:" + com) ;    // 调用的是toString()方法
    }
};
class BinaryTree{
    class Node{            // 声明一个节点类
        private Comparable data ;    // 保存具体的内容
        private Node left ;            // 保存左子树
        private Node right ;        // 保存右子树
        public Node(Comparable data){
            this.data = data ;
        }
        public void addNode(Node newNode){
            // 确定是放在左子树还是右子树
            if(newNode.data.compareTo(this.data)<0){    // 内容小,放在左子树
                if(this.left==null){
                    this.left = newNode ;    // 直接将新的节点设置成左子树
                }else{
                    this.left.addNode(newNode) ;    // 继续向下判断
                }
            }
            if(newNode.data.compareTo(this.data)>=0){    // 放在右子树
                if(this.right==null){
                    this.right = newNode ;    // 没有右子树则将此节点设置成右子树
                }else{
                    this.right.addNode(newNode) ;    // 继续向下判断
                }
            }
        }
        public void printNode(){    // 输出的时候采用中序遍历
            if(this.left!=null){
                this.left.printNode() ;    // 输出左子树
            }
            System.out.print(this.data + "\t") ;
            if(this.right!=null){
                this.right.printNode() ;
            }
        }
    };
    private Node root ;        // 根元素
    public void add(Comparable data){    // 加入元素
        Node newNode = new Node(data) ;    // 定义新的节点
        if(root==null){    // 没有根节点
            root = newNode ;    // 第一个元素作为根节点
        }else{
            root.addNode(newNode) ; // 确定是放在左子树还是放在右子树
        }
    }
    public void print(){
        this.root.printNode() ;    // 通过根节点输出
    }
};
public class ComparableDemo03{
    public static void main(String args[]){
        BinaryTree bt = new BinaryTree() ;
        bt.add(8) ;
        bt.add(3) ;
        bt.add(3) ;
        bt.add(10) ;
        bt.add(9) ;
        bt.add(1) ;
        bt.add(5) ;
        bt.add(5) ;
        System.out.println("排序之后的结果:") ;
        bt.print() ;
    }
};
import java.util.* ;
class Student{    // 指定类型为Student
    private String name ;
    private int age ;
    public Student(String name,int age){
        this.name = name ;
        this.age = age ;
    }
    public boolean equals(Object obj){    // 覆写equals方法
        if(this==obj){
            return true ;
        }
        if(!(obj instanceof Student)){
            return false ;
        }
        Student stu = (Student) obj ;
        if(stu.name.equals(this.name)&&stu.age==this.age){
            return true ;
        }else{
            return false ;
        }
    }
    public void setName(String name){
        this.name = name ;
    }
    public void setAge(int age){
        this.age = age ;
    }
    public String getName(){
        return this.name ;
    }
    public int getAge(){
        return this.age ;
    }
    public String toString(){
        return name + "\t\t" + this.age  ;
    }
};

class StudentComparator implements Comparator<Student>{    // 实现比较器
    // 因为Object类中本身已经有了equals()方法
    public int compare(Student s1,Student s2){
        if(s1.equals(s2)){
            return 0 ;
        }else if(s1.getAge()<s2.getAge()){    // 按年龄比较
            return 1 ;
        }else{
            return -1 ;
        }
    }
};

public class ComparatorDemo{
    public static void main(String args[]){
        Student stu[] = {new Student("张三",20),
            new Student("李四",22),new Student("王五",20),
            new Student("赵六",20),new Student("孙七",22)} ;
        java.util.Arrays.sort(stu,new StudentComparator()) ;    // 进行排序操作
        for(int i=0;i<stu.length;i++){    // 循环输出数组中的内容
            System.out.println(stu[i]) ;
        }
    }
};

猜你喜欢

转载自www.cnblogs.com/tszr/p/12152941.html