实验五集合的使用讲述.doc
文本预览下载声明
EXP3
课题(项目)名称: 实验五 集合的使用 计划学时: 2 实验类型: 1.演示性□ 2.验证性□ 3.综合性□ 4.设计性□ 5.其它□ 授课日期: 年 月 日 第 周 星期 第 节 实验目的
验证集合的使用
学习集合的应用技术
实验要求
掌握List的使用
掌握Set的使用
掌握Map的使用
理解集合应用技术
实验内容与步骤
集合基本操作
(1)编写存入集合的对象类
class Person {
String id;
String name;
Person(String id,String name)
{
this.id=id;
this.name=name;
}
public void sleep() {
System.out.println(I am Person,I am sleeping);
}
public void eat() {
System.out.println(I am Person,I am eating);
}
}
class Student extends Person {
String sno;
Student(String id,String name,String sno)
{
super(id,name);
this.sno=sno;
}
public void study() {
System.out.println(I am Student,I am studying);
}
public void eat() {
System.out.println(I am Student);
System.out.println(id:+id);
System.out.println(name:+name);
System.out.println(sno:+sno);
System.out.println(eating ..........);
}
public String toString(){
return Student +ID:+this.id
+ Name:+this.name
+ SNo:+this.sno;
}
}
class Teacher extends Person {
String tid;
Teacher(String id,String name,String tid)
{
super(id,name);
this.tid=tid;
}
public void tech() {
System.out.println(I am Student,I am taching);
}
public void eat() {
System.out.println(I am Teacher);
System.out.println(id:+id);
System.out.println(name:+name);
System.out.println(tid:+tid);
System.out.println(eating ..........);
}
public String toString(){
return Teacher +ID:+this.id
+ Name:+this.name
+ Tid:+this.tid;
}
}
(2)创建测试类,完成以下操作
a.创建3个Student的对象,2个Teacher对象
b.创建一个List和一个Set
c.将a中创建的5个对象加入到List中
d.将a中创建的5个对象加入到Set中
e.编写一个static方法,可以对b中中创始的集合进行遍历,将集合中每个元素读出来转型为Person对象,并调用eat()方法
public static void travel(Collection collection){
Iterator it = collection.iterator();
}
f.尝试删除List和Set中的元素,并在删除前后都调用e中编写的方法对List,Set进行遍历.
g.创建一个Map,并将a中创建的三个学生对象,加入到Map中(以SNo为Key)
HashMap
h.编写一个static 方法,实现对g中创建Map进行遍历,并调用study()方法
public static void travel(Map map){
}
i.编写代码测试Set中不能加入重复元素
package Exercise1;
import java.util.*;
public class Persontest {
public static void main(St
显示全部