对象排序的两种实例
来源:互联网 发布: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 + "元]"; }}
阅读全文
0 0
- 对象排序的两种实例
- String类的两种实例化对象的方式
- python list排序的两种方法及实例讲解
- python list排序的两种方法及实例讲解
- Python list排序的两种方法及实例讲解
- python list排序的两种方法及实例讲解
- Java实现对象排序的两种方法
- 数组对象的Arrays排序两种方式
- 集合中自定义对象排序的两种方式
- python中对象排序的两种方式
- Java按照对象属性排序的两种方法
- java-Collections对象的降序排序--实例
- java-Collections对象的降序排序--实例
- python list列表排序的两种方法及实例讲解
- 自定义类的对象作为TreeSet元素的两种方法排序浅谈
- 对象的两种写法
- TreeSet存储自定义对象,并对对象排序的两种方式
- 黑马程序员训练营---------TreeSet集合中对象进行排序的两种方式
- 全国省份城市级联下拉菜单
- [App] MongoDB 单点安装及应用
- three.js 04-10 之 LineDashedMaterial 材质
- 20170628
- java--面向对象之封装(习题)
- 对象排序的两种实例
- 【LeetCode】216.Combination Sum III(Medium)解题报告
- JSON.stringify
- [App] MongoDB 主从配置
- 四旋翼定高篇之惯导加速度+速度+位置三阶互补融合方案
- 在windwos环境下用gcc编译boost动态库
- myblog 中的bug处理
- ros相关
- ERROR: Unknown command 'crunch' 的解决方法