实验11泛型与集合

上传人:壹****1 文档编号:470621153 上传时间:2023-01-30 格式:DOC 页数:4 大小:286KB
返回 下载 相关 举报
实验11泛型与集合_第1页
第1页 / 共4页
实验11泛型与集合_第2页
第2页 / 共4页
实验11泛型与集合_第3页
第3页 / 共4页
实验11泛型与集合_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《实验11泛型与集合》由会员分享,可在线阅读,更多相关《实验11泛型与集合(4页珍藏版)》请在金锄头文库上搜索。

1、【精品文档】如有侵权,请联系网站删除,仅供学习与交流实验11泛型与集合.精品文档.实验内容:(1) 定义一个泛型类 Instrument,其中包括一个泛型方法 void play(E x)。定义两种乐器类:Cello、Violin 可以进行演奏。定义一个测试类进行测试。package ex111;public class Violin public String toString() System.out.println(violin.); return ; package ex111;public class Test_Main public static void main(String

2、args) instrumentmodel = new instrument(); instrumentmodel1 = new instrument(); Cello cello = new Cello(); Violin violin = new Violin(); model.play(cello); model1.play(violin); package ex111;class instrument /泛型类 void play(E x) x.toString(); package ex111;public class Cello public String toString()Sy

3、stem.out.println(cello.);return ;(2) 输入 10 个数字保存到 List 中,并按倒序显示出来。package ex112;import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.Scanner;public class Testpublic static void main(final String args) final List integers = new ArrayList(); final Scanner scan

4、ner = new Scanner(System.in); for (int i = 1; i = 3; i+) try System.out.println(输入第 + i + 个数吧); final int input = Integer.parseInt(scanner.next(); integers.add(input); catch (final Throwable e) System.err.println(这不是个数字,我可是超级程序); i-; continue; /Collections.sort(integers);/ 自然排 Collections.reverse(in

5、tegers);/ 倒排 for (final Integer integer : integers) System.out.println(integer); System.out.println(integer);package ex113;import java.util.HashMap; import java.util.Map; public class Test public static void main(String args) Map map=new HashMap(); Sort s=new Sort(); s.creatMap(map); s.Sort(map); (3

6、) 编写一个程序,把学生名和考试分数录入到 Map 中,并按分数显示前三名学生的名字。要求定义 Student 类,封装学生名和考试分数 2 个属性及方法。、TreeMap、 TreeSet;package ex113;import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; public cla

7、ss Student private String name; private int score; public Student() public Student(String name,int score) this.name=name; this.score=score; public String getName() return name; public void setName(String name) this.name = name; public int getScore() return score; public void setScore(int score) this

8、.score = score; package ex113;import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; public class Sort public void creatMap(Map map) Scanner input=ne

9、w Scanner(System.in); System.out.println(请输入学生人数); int number=input.nextInt(); System.out.println(请输入学生姓名和分数:); for(int i=0;inumber;i+) Student stu=new Student(input.next(),input.nextInt(); map.put(stu.getName(),stu.getScore(); public void Sort(Map map) Collection score = map.values(); Set set=new HashSet(); for(Integer s:score) set.add(s); Listlist=new ArrayList(set); Collections.sort(list); Collections.reverse(list); Set name = map.keySet(); System.out.println(前三名学生的姓名是:); int m=0; for(Integer i:list) m+; if(m=3) for(String s:name) if(i=map.get(s) System.out.println(第+m+名:+s);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号