选择题
1.B
2.A
3.D
4.AC
5.A
6.D
7.C
8.ABC
9.C
10.A
11.C
编程题
- 使用集合ArrayList对字符串进行存储和管理。
任务:
- 定义ArrayList对象
- 存储学科名称,见运行效果图
- 输出集合中元素的个数
- 遍历输出集合中的所有元素 编程:
public class ArrayListDemo {
public static void main(String[] args) {
//用ArrayList存储学科的名称
List<String> list = new ArrayList<String>();
list.add("第一个为语文");
list.add("第二个为数学");
list.add("第三个为英语");
list.add("第四个为化学");
list.add("第五个为物理");
list.add("第六个为生物");
//输出列表中元素的个数
System.out.println(list.size());
//遍历输出所有列表元素
for(String a : list) {
System.out.println(a);
}
}
}
- 定义一个员工信息类Employee,使用ArrayList对员工信息进行添加和显示。
1、实现员工信息类Employee 成员变量:编号id(int),姓名name(String),薪资salary(double) 方法:构造方法和相关的get和set方法
2、定义三条员工信息添加到ArrayList中
3、将所有员工的姓名和薪资输出,见效果图
public class Employee {
private int id;
private String name;
private double salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Employee() {
super();
}
public Employee(int id, String name, double salary) {
super();
this.id = id;
this.name = name;
this.salary = salary;
}
public static void main(String[] args) {
//定义ArrayList对象
List<Employee> list = new ArrayList<Employee>();
//创建三个Employee类的对象
Employee e1 = new Employee(1, "张三", 5000.0);
Employee e2 = new Employee(1, "李四", 5500.0);
Employee e3 = new Employee(1, "赵六", 4000.0);
//添加员工信息到ArrayList中
list.add(e1);
list.add(e2);
list.add(e3);
//显示员工的姓名和薪资
System.out.println("员工姓名 员工薪资");
for(int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName() + " " + list.get(i).getSalary());
}
}
}
- 定义一个学生类,使用HashSet对学生类的对象进行管理:执行添加操作,然后解决重复数据的 添加问题。
任务:
1、定义一个学生类Student (1)属性为:学号stuId(int),姓名name(String),成绩score(float) (2)方法为:构造方法,getter和setter方法,toString方法 (3)重写hashCode()和equals()方法,equals方法的判断依据是学号和姓名相等
2、 定义三个Student类的对象,添加到HashSet中
3.、显示HashSet中元素的内容
4、添加一个重复数据到Set中,观察输出结果
编程:
public class Student {
private int stuld;
private String name;
private float score;
public int getStuld() {
return stuld;
}
public void setStuld(int stuld) {
this.stuld = stuld;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
public Student() {
super();
}
public Student(int stuld, String name, float score) {
super();
this.stuld = stuld;
this.name = name;
this.score = score;
}
@Override
public String toString() {
return "[学号:" + stuld + ", 姓名:" + name + ", 成绩:" + score + "]";
}
@Override
public boolean equals(Object arg0) {
// TODO Auto-generated method stub
if(this == arg0) {
return true;
}
if(arg0.getClass() == Student.class) {
Student stu = (Student)arg0;
return stu.getName().equals(this.getStuld());
} else {
return false;
}
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return super.hashCode();
}
}
public class StudentTest {
public static void main(String[] args) {
//定义三个Student类的对象及一个HashSet类的对象
Student s1 = new Student(1, "Tom", 87.0f);
Student s2 = new Student(2, "Lucy", 95.0f);
Student s3 = new Student(3, "William", 65.0f);
Set<Student> hashset = new HashSet<Student>();
//将Student类的对象添加到集合中
hashset.add(s1);
hashset.add(s2);
hashset.add(s3);
//使用迭代器显示Student类的对象中的内容
Iterator it = hashset.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
- 已知如下数据:世界杯冠军及夺冠年份。
将夺冠年份作为key值,冠名队名作为value值,存储至少三条数据到HashMap中,并循环打印输出。
任务:
1、将夺冠年份和冠军队名以key-value形式存储到HashMap中
2、使用迭代器和EntrySet两种方式遍历输出HashMap中的key和value
编程:
public class FootballDemo {
public static void main(String[] args) {
//定义HashMap的对象并添加数据
// Scanner sc = new Scanner(System.in);
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(2006, "意大利");
map.put(2014, "德国");
map.put(2010, "西班牙");
// int i = 0;
// while(i < 3) {
// System.out.println("请输入年份:");
// int num = sc.nextInt();
// System.out.println("请输入冠军队伍:");
// String s = sc.next();
// map.put(num, s);
// i++;
// }
//使用迭代器的方式遍历
Iterator it = map.values().iterator();
System.out.println("使用迭代器方式进行输出:");
while(it.hasNext()) {
System.out.print(it.next() + " ");
}
System.out.println();
//使用EntrySet同时获取key和value
Set<Entry<Integer, String>> entrySet = map.entrySet();
System.out.println("使用EntrySet进行输出:");
for(Entry<Integer, String> entry : entrySet) {
System.out.print(entry.getKey() + "-");
System.out.println(entry.getValue());
}
}
}