对象排序的两种实例

来源:互联网 发布:java获取jquery ip 编辑:程序博客网 时间:2024/06/14 17:04

作者:叁念


对象排序的两种实例

如何实现对象的排序?本文章为实例,理论请先看此文章(点击跳转)


1.方式一,以数组进行排序实现


Car类

public class Car implements Comparable<Car>{     /**     * 属性     */    private String name;    //名称    private double weight;  //重量    private double length;  //长度    private double carryNumber; //载数    private double maxCarryWeight;  //载重    private double price; //价格    /**     * 构造器     */    public Car(String name, double weight, double length, double carryNumber, double maxCarryWeight, double price) {        super();        this.name = name;        this.weight = weight;        this.length = length;        this.carryNumber = carryNumber;        this.maxCarryWeight = maxCarryWeight;        this.price = price;    }    /**     * get/set     */    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public double getWeight() {        return weight;    }    public void setWeight(double weight) {        this.weight = weight;    }    public double getLength() {        return length;    }    public void setLength(double length) {        this.length = length;    }    public double getCarryNumber() {        return carryNumber;    }    public void setCarryNumber(double carryNumber) {        this.carryNumber = carryNumber;    }    public double getMaxCarryWeight() {        return maxCarryWeight;    }    public void setMaxCarryWeight(double maxCarryWeight) {        this.maxCarryWeight = maxCarryWeight;    }    public double getPrice() {        return price;    }    public void setPrice(double price) {        this.price = price;    }    /**     * 重写to String     */    @Override    public String toString() {        return "Car [名称:" + name + ", 重量=" + weight + "吨, 长度=" + length + "米, 载数=" + carryNumber                + "人, 载重=" + maxCarryWeight + "吨, 价格=" + price + "元]";    }    @Override    public int compareTo(Car o2) {        // 相等返回0 大于返回1 小于返回-1        if (this.getPrice() == o2.getPrice()) {            if (this.getMaxCarryWeight() == o2.getMaxCarryWeight()) {                if (this.getCarryNumber() == o2.getCarryNumber()) {                    if (this.getWeight() == o2.getWeight()) {                        if (this.getLength() == o2.getLength()) {                            return 0;                        } else {                            return this.getLength() > o2.getLength() ? 1 : -1;// Length基准                        }                    } else {                        return this.getWeight() > o2.getWeight() ? 1 : -1;// Weight基准                    }                } else {                    return this.getCarryNumber() > o2.getCarryNumber() ? 1 : -1;// CarryNumber基准                }            } else {                return this.getMaxCarryWeight() > o2.getMaxCarryWeight() ? 1 : -1;// MaxCarryWeight基准            }        } else {            return this.getPrice() > o2.getPrice() ? 1 : -1;// Price基准        }    }}

Demo 类

import java.util.Arrays;import java.util.Scanner;/** * 缺点:数组暂且没有扩容 * @author yujie * */public class Demo {    static Car [] arrCar = new Car[10];    public static void main(String[] args) {        boolean isRun = true;        Scanner sc = new Scanner(System.in);        // 创建一个数值list        // 添加并显示对象(无序) new Car(名称, 重量, 长度, 载数, 载重, 价格)        for (int i = 0; i < 10; i++) {            arrCar[i] = new Car(i + 1 + "号", getNumber(), getNumber(), getNumber(), getNumber(), getNumber() * 99);        }        int select = 0;        while (isRun) {            System.out.println("\n请输入你要进行的操作:1显示所有信息  ,2查询车辆信息 , 3修改车辆信息  4 显示所有信息(有序)————其他数字退出程序");            select = sc.nextInt();            switch (select) {            case 1:                showAll();                break;            case 2:                System.out.println("请输入你要查询的车辆名字如(1号)");                System.out.println(findCar(sc.next()));                break;            case 3:                System.out.println("请输入你要修改的车辆名字如(1号)");                String oldName = sc.next();                System.out.println("请输入你要变化的名字");                String newName = sc.next();                reviseCar(oldName, newName);                break;            case 4:                Arrays.sort(arrCar);                showAll();                break;            default:                isRun = false;                break;            }        }    }    /**     * getNumber() 得到一个随机数     *      * @return     */    public static int getNumber() {        return (int) (Math.random() * 100);    }    /**     * findCar(String name) 根据名称name查找车信息     *      * @param name     */    public static Car findCar(String name) {        for (Car car : arrCar) {            if (name.equals(car.getName())) {                return car;            }        }        return null;    }    /**     * reviseCar(String name) 根据名称name修改车信息     *      * @param name     */    public static void reviseCar(String oldName, String newName) {        findCar(oldName).setName(newName);    }    public static void showAll() {        for (Car car : arrCar) {            if(car != null) {                System.out.println(car);            }        }    }}

2.方式二,以数组链表进行排序实现

Demo 类

import java.util.ArrayList;import java.util.Comparator;import java.util.Scanner;public class Demo {    static ArrayList<Car> arrayList = new ArrayList<>();    public static void main(String[] args) {        boolean isRun = true;        Scanner sc = new Scanner(System.in);        // 创建一个数值list        // 添加并显示对象(无序) new Car(名称, 重量, 长度, 载数, 载重, 价格)        for (int i = 0; i < 10; i++) {            arrayList.add(new Car(i + 1 + "号", getNumber(), getNumber(), getNumber(), getNumber(), getNumber() * 99));        }        int select = 0;        while (isRun) {            System.out.println("\n请输入你要进行的操作:1显示所有信息  ,2查询车辆信息 , 3修改车辆信息  4 显示所有信息(有序)————其他数字退出程序");            select = sc.nextInt();            switch (select) {            case 1:                showAll();                break;            case 2:                System.out.println("请输入你要查询的车辆名字如(1号)");                System.out.println(findCar(sc.next()));                break;            case 3:                System.out.println("请输入你要修改的车辆名字如(1号)");                String oldName = sc.next();                String newName = sc.next();                reviseCar(oldName, newName);                break;            case 4:                arrayList.sort(new Comparator<Car>() {                    @Override                    public int compare(Car o1, Car o2) {                        // 相等返回0 大于返回1 小于返回-1                        if (o1.getPrice() == o2.getPrice()) {                            if (o1.getMaxCarryWeight() == o2.getMaxCarryWeight()) {                                if (o1.getCarryNumber() == o2.getCarryNumber()) {                                    if (o1.getWeight() == o2.getWeight()) {                                        if (o1.getLength() == o2.getLength()) {                                            return 0;                                        } else {                                            return o1.getLength() > o2.getLength() ? 1 : -1;// Length基准                                        }                                    } else {                                        return o1.getWeight() > o2.getWeight() ? 1 : -1;// Weight基准                                    }                                } else {                                    return o1.getCarryNumber() > o2.getCarryNumber() ? 1 : -1;// CarryNumber基准                                }                            } else {                                return o1.getMaxCarryWeight() > o2.getMaxCarryWeight() ? 1 : -1;// MaxCarryWeight基准                            }                        } else {                            return o1.getPrice() > o2.getPrice() ? 1 : -1;// Price基准                        }                    }                });                showAll();                break;            default:                isRun = false;                break;            }        }    }    /**     * getNumber() 得到一个随机数     *      * @return     */    public static int getNumber() {        return (int) (Math.random() * 100);    }    /**     * findCar(String name) 根据名称name查找车信息     *      * @param name     */    public static Car findCar(String name) {        for (Car car : arrayList) {            if (name.equals(car.getName())) {                return car;            }        }        return null;    }    /**     * reviseCar(String name) 根据名称name修改车信息     *      * @param name     */    public static void reviseCar(String oldName, String newName) {        findCar(oldName).setName(newName);        ;    }    public static void showAll() {        for (Car car : arrayList) {            System.out.println(car);        }    }}

Car类

public class Car {     /**     * 属性     */    private String name;    //名称    private double weight;  //重量    private double length;  //长度    private double carryNumber; //载数    private double maxCarryWeight;  //载重    private double price; //价格    /**     * 构造器     */    public Car(String name, double weight, double length, double carryNumber, double maxCarryWeight, double price) {        super();        this.name = name;        this.weight = weight;        this.length = length;        this.carryNumber = carryNumber;        this.maxCarryWeight = maxCarryWeight;        this.price = price;    }    /**     * get/set     */    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public double getWeight() {        return weight;    }    public void setWeight(double weight) {        this.weight = weight;    }    public double getLength() {        return length;    }    public void setLength(double length) {        this.length = length;    }    public double getCarryNumber() {        return carryNumber;    }    public void setCarryNumber(double carryNumber) {        this.carryNumber = carryNumber;    }    public double getMaxCarryWeight() {        return maxCarryWeight;    }    public void setMaxCarryWeight(double maxCarryWeight) {        this.maxCarryWeight = maxCarryWeight;    }    public double getPrice() {        return price;    }    public void setPrice(double price) {        this.price = price;    }    /**     * 重写to String     */    @Override    public String toString() {        return "Car [名称:" + name + ", 重量=" + weight + "吨, 长度=" + length + "米, 载数=" + carryNumber                + "人, 载重=" + maxCarryWeight + "吨, 价格=" + price + "元]";    }}
原创粉丝点击