实验11泛型与集合
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验内容:
(1)定义一个泛型类In strume nt
package ex111;
public class Violin {
public String toStri ng()
{
System.out.println("violin ....... ");
return "";
} }
package ex111;
public class Test_Ma in {
public static void main( Stri ng[] args) {
instrument
model1 = new in strume ntvVioli n>(); Cello cello = new Cello();
Violin violin = new Violi n();
model.play(cello);
model1.play(violi n);
}
}
package ex111;
class in strume nt
{
x.toStri ng();
} }
package ex111;
public class Cello {
public Stri ng toStri ng(){
System.out.println("cello ....... ");
return "";
}
}
(2)输入10个数字保存到List中,并按倒序显示出来
package ex112;
import java.util.ArrayList;
import java.util.Collectio ns;
import java.util.List;
import java.util.Sca nner; public class Test{
public static void main(final String[] args) {
final List< In teger> in tegers = new ArrayListvl nteger>();
final Scanner sca nner = new Scann er(System.i n);
for (int i = 1; i <= 3; i++) {
try {
System.out.pri ntl n("输入第"+ i + "个数吧");
final int in put = In teger.parse In t(sca nner.n ext());
in tegers.add(i nput);
}
catch (final Throwable e) {
System.err.pri ntln (”这不是个数字,我可是超级程序");
i--;
con ti nue;
}
}
//Collections.sort(integers);// 自然排
Collect ion s.reverse(i ntegers);// 倒排
for (final In teger in teger : in tegers) {
System.out.pri ntln (i nteger);
}
}
} System.out.pri ntln (i nteger);
}
}
}
package ex113;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main( Stri ng[] args) {
Map
s=new Sort();
s.creatMap(map);
s.Sort(map);
}
}
(3)编写一个程序,把学生名和考试分数录入到Map中,并按分数显示前
三名学生的名字。要求定义Stude nt类,封装学生名和考试分数2个属性及方法。、TreeMap TreeSe;package ex113; import java.util.ArrayList;
import java.util.Collectio n;
import java.util.Collectio ns;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Stude nt {
private String n ame; private int score; public Stude nt(){ }
public Stude nt(Stri ng n ame,i nt score) { this .n ame=n ame;
this.score=score;
}
public Stri ng getName() { return n ame;
}
public void setName(Stri ng n ame) { this. name = n ame;
}
public int getScore() {
return score;
}
public void setScore(i nt score) { this.score = score;
}
}
package ex113;
import java.util.ArrayList;
import java.util.Collectio n;
import java.util.Collectio ns;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Sca nner;
import java.util.Set;
public class Sort {
public void creatMap(Map
Scanner in put=new Scann er(System.i n);
System.out.println("请输入学生人数");
int nu mber =in put. nextl nt();
System.out.println(”请输入学生姓名和分数:");
for(int i=0;i Stude nt stu=new Stude nt(i nput. next(),i nput .n extI nt()); map.put(stu.getName(),stu.getScore()); } } public void Sort(Map Collecti onvln teger> score = map.values(); Set< In teger> set=new HashSetv In teger>(); for(I nteger s:score) set.add(s); List< In teger>list=new ArrayList(set);