填空题
利用实现接口Comparator<T>的类,重写compare(To1, To2)方法,以此作为TreeMap的排序规则。即按照TreeMap的构造方法public TreeMap(Comparator super K>comparator)来创建映像树TreeMap的对象。达到建立有序的映像树结构。存入7名同学的信息(学号、姓名、身高、体重、Java成绩、数据结构成绩,可以有相同的姓名)。要求实现按照姓名+身高+总成绩三个属性由大到小排序(即总成绩相同时按姓名由大到小排序,总成绩和姓名都相同情况下按身高由大到小排序)。import static java.lang.System.*;import java.util.*;//CompareCom实现接口Comparator中的方法compare,//按照Student对象的name,height和score域的从小到大排序class CompareCom implements Comparator{public int compare(String s1, String s2){【代码1】//添加代码,实现compare方法}}class Student{String name=null;int no,height,weight;float score;Student(int n,String name,int h,int w,float sc){no=n;this.name=name;height=h;weight=w;score=sc;}}public class Example7_14xitiX{public static void main(String args[]){Student st1,st2,st3,st4,st5,st6,st7;st1=new Student(1,"zhanying",182,80,95.5f);st2=new Student(2,"wangheng",170,70,98f);st3=new Student(3,"Liuqing",175,60,78f);st4=new Student(4,"zhanying",172,80,85.5f);st5=new Student(5,"wanheng",165,70,92f);st6=new Student(6,"wuquan",162,60,78f);st7=new Student(7,"Lufen",175,60,98.5f);CompareCom compareCom=new CompareCom();//排序树元素与定制的排序方式(按姓名+身高+成绩排序)关联TreeMaptree=new TreeMap(compareCom);tree.put(st1.name+Integer.toString(st1.height)+Float.toString(st1.score),st1);tree.put(st2.name+Integer.toString(st2.height)+Float.toString(st2.score),st2);tree.put(st3.name+Integer.toString(st3.height)+Float.toString(st3.score),st3);tree.put(st4.name+Integer.toString(st4.height)+Float.toString(st4.score),st4);tree.put(st5.name+Integer.toString(st5.height)+Float.toString(st5.score),st5);tree.put(st6.name+Integer.toString(st6.height)+Float.toString(st6.score),st6);tree.put(st7.name+Integer.toString(st7.height)+Float.toString(st7.score),st7);Collection collection=tree.values();Iterator te=collection.iterator();out.printf("\n\n");out.println("按姓名从小到大排序后的结果是: ");out.println(" 姓名身高成绩体重 ");while(te.hasNext()){Student stu=te.next();out.printf("%-15s %5d %10.2f %6d\n",stu.name,stu.height,stu.score,stu.weight);}}}
套餐购买该问题答案仅对会员开放,欢迎开通会员 ¥ 19.9
0.64/天
1个月(不限次)
¥ 19.9
1000次
(不限时)
¥ 29.9
0.32/天
3个月(不限次)
¥ 59.9
0.16/天
1年(不限次)
立即支付