《学士后Java集合框架和泛型习题答案.docx》由会员分享,可在线阅读,更多相关《学士后Java集合框架和泛型习题答案.docx(5页珍藏版)》请在三一办公上搜索。
1、学士后Java集合框架和泛型习题答案package entity; /学生类,属性为:姓名、性别、分数、年龄、班级 public class Student private String name; private String gender; private int score; private int age; private String classes; public Student(String name,String gender, int score,String classes) this.name = name; this.gender = gender; this.scor
2、e = score; this.classes = classes; public Student(String name, String gender, int age, int score) this.name = name; this.gender = gender; this.age = age; this.score = score; public Student(String name, int score) this.name = name; this.score = score; public Student(String name) this.name = name; pro
3、tected String getName return name; protected void setName(String name) this.name = name; protected int getAge return age; protected void setAge(int age) this.age = age; protected String getGender return gender; protected void setGender(String gender) this.gender = gender; protected int getScore retu
4、rn score; protected void setScore(int score) this.score = score; protected String getClasses return classes; protected void setClasses(String classes) this.classes = classes; 第二题答案源码及运行效果截图: package entity; import java.util.HashMap; import java.util.Scanner; public class Homework2 public static void
5、 main(String args) HashMap students=new HashMap; Scanner input=new Scanner(System.in); Student stu1=new Student(杨过, 95); Student stu2=new Student(小龙女, 99); Student stu3=new Student(张无忌, 88); Student stu4=new Student(赵敏, 90); students.put(杨过, stu1); students.put(小龙女, stu2); students.put(张无忌, stu3); s
6、tudents.put(赵敏, stu4); System.out.println(请输入学生姓名:); String key=input.next; if(students.containsKey(key) Student stu= students.get(key); System.out.println(stu.getName+的成绩是:+stu.getScore); System.out.println(-所有学生的成绩是-); for(Object value:students.values) Student stu=(Student) value; System.out.print
7、ln(stu.getName+的成绩是:+stu.getScore); 第三题答案源码及运行效果截图: package entity; import java.util.*; public class Homework3 public static void main(String args) List list1=new ArrayList; List list2=new ArrayList; List list3=new ArrayList; Student stu1=new Student(张三); Student stu2=new Student(李四); Student stu3=n
8、ew Student(王五); Student stu4=new Student(成龙); Student stu5=new Student(李娜); Student stu6=new Student(张杰); Student stu7=new Student(刘德华); Student stu8=new Student(张学友); Student stu9=new Student(刘亦菲); list1.add(stu1); list1.add(stu2); list1.add(stu3); list2.add(stu4); list2.add(stu5); list2.add(stu6);
9、 list3.add(stu7); list3.add(stu8); list3.add(stu9); MapString,List map=new HashMapString,List; map.put(三年级一班, list1); map.put(三年级二班, list2); map.put(三年级三班, list3); System.out.println(输入班级名称:); Scanner input=new Scanner(System.in); String banji=input.next; System.out.println(banji+学生列表:); List list=m
10、ap.get(banji); for(Student stu:list) System.out.println(stu.getName); 第四题答案源码及运行效果截图: package entity; import java.util.*; import java.util.Scanner; public class Homework4 public static void main(String args) ArrayList list1=new ArrayList; ArrayList list2=new ArrayList; ArrayList list3=new ArrayList;
11、 Student stu1=new Student(张三,男,20,85); Student stu2=new Student(李四,男,22,80); Student stu3=new Student(王五,男,24,88); Student stu4=new Student(成龙,男,26,95); Student stu5=new Student(李娜,女,24,99); Student stu6=new Student(张杰,男,23,88); Student stu7=new Student(刘德华,男,28,99); Student stu8=new Student(张学友,男,2
12、7,87); Student stu9=new Student(刘亦菲,女,24,97); list1.add(stu1); list1.add(stu2); list1.add(stu3); list2.add(stu4); list2.add(stu5); list2.add(stu6); list3.add(stu7); list3.add(stu8); list3.add(stu9); MapString,ArrayList map=new HashMapString,ArrayList; map.put(三年级一班, list1); map.put(三年级二班, list2); map.put(三年级三班, list3); System.out.println(输入班级名称:); Scanner input=new Scanner(System.in); String banji=input.next; System.out.println(banji+学生列表:); ArrayList list=map.get(banji); for(Student stu:list) System.out.println(stu.getName+t+stu.getGender+t+stu.getAge);