背景:将一个数组排序,并且打印;
1.下面使用选择排序、和冒泡排序来写:                        
public class Test {

    public static void main(String[] args) {
        int a[] = {9, 5, 3, 7, 1, 10};
        DataSorter.sort(a);
        DataSorter.p(a);
    }

}

DataSort:

package com.cy.dp.strategy;

public class DataSorter {
    
    /**
     * 选择排序法
     * @param a
    public static void sort(int[] a) {
        int temp;
        for(int i=0; i<a.length; i++){
            for(int j=i+1; j<a.length; j++){
                if(a[i]>a[j]){
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
    } */
    
    /**
     * 冒泡排序法
     * @param a
     */
    public static void sort(int[] a) {
        int temp;
        for(int i=a.length; i>0; i--){
            for(int j=0; j<i-1; j++){
                if(a[j]>a[j+1]){
                    temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
    }

    public static void p(int[] a) {
        for(int i=0; i<a.length; i++){
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }
    
    
}

 

但是上面有个问题:

sort方法里面现在只能排序int类型的;

如果我想对于float类型进行排序,怎么办?--可以重载方法sort(float[] a);

如果想对于double类型的进行排序,怎么办?--再重载一个double参数的;

因为sort方法里面a[j]和a[j+1]的int类型的,能直接比较,这还好办,但是如果现在想DataSorter对猫、狗、Car、等等...进行排序,怎么办?

 
2.对所有对象的数组都能够进行排序
定义比较接口Comparable:
//实现这个接口的类,表示是可以比较的
public interface Comparable {
    
    public int compareTo(Object o);
}

DataSort可以排序任何类(该类实现Comparable接口),打印任何类;

写完一次排序方法,再也不用改变了;

package com.cy.dp.strategy;

public class DataSorter {
    
    /**
     * 冒泡排序法,排序任何类型,Object类型
     * 假设Object[]里面对象都实现了Comparable接口,都是可以比较大小的;
     * @param a
     */
    public static void sort(Object[] a) {
        Object temp;
        for(int i=a.length; i>0; i--){
            for(int j=0; j<i-1; j++){
                Comparable o1 = (Comparable)a[j];
                Comparable o2 = (Comparable)a[j+1];
                if(o1.compareTo(o2) > 0){
                    temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
    }

    public static void p(Object[] a) {
        for(int i=0; i<a.length; i++){
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }
    
    
}

Cat实现Compareble接口:

package com.cy.dp.strategy;

public class Cat implements Comparable{
    private int height;
    private int weight;
    
    public Cat(int height, int weight) {
        super();
        this.height = height;
        this.weight = weight;
    }
    public int getHeight() {
        return height;
    }
    public void setHeight(int height) {
        this.height = height;
    }
    public int getWeight() {
        return weight;
    }
    public void setWeight(int weight) {
        this.weight = weight;
    }
    
    @Override
    public int compareTo(Object o) {
        if(o instanceof Cat){
            Cat c = (Cat) o;
            if(this.getHeight()>c.getHeight()) return 1;
            else if(this.getHeight() < c.getHeight()) return -1;
            else return 0;
        }
        return -100;    //简单写法,如果o不是cat,表示出错;实际要抛异常
    }
    
    
    @Override
    public String toString() {
        return height + "|" + weight;
    }
}
View Code

Dog实现Comparable接口:

package com.cy.dp.strategy;

public class Dog implements Comparable{
    private int food;
    
    public Dog(int food) {
        super();
        this.food = food;
    }
    public int getFood() {
        return food;
    }
    public void setFood(int food) {
        this.food = food;
    }
    
    //对狗按照饭量大小进行排序
    @Override
    public int compareTo(Object o) {
        Dog d = (Dog) o;
        if(this.food > d.getFood()) return 1;
        else if(this.food < d.getFood()) return -1;
        else return 0;
    }
    @Override
    public String toString() {
        return "Dog [food=" + food + "]";
    }
    
}
View Code

相关文章:

  • 2021-05-02
  • 2022-01-27
  • 2021-06-30
  • 2021-10-30
  • 2021-12-27
  • 2022-12-23
猜你喜欢
  • 2022-01-18
  • 2021-09-25
  • 2021-11-23
  • 2021-04-13
  • 2022-01-06
  • 2021-12-07
  • 2021-07-05
  • 2021-07-07
相关资源
相似解决方案