练习1
练习2
package JUL.JUL.Jul17; import java.util.*; public class Demo01 { static Scanner input = new Scanner(System.in); // 创建数组 public List addList(String[] strs) { List<String> list = new ArrayList<String>(); for (int i = 0; i < strs.length; i++) { list.add(strs[i]); } return list; } public static void main(String[] args) { Map<String, List> map = new HashMap<String, List>(); String[] str1 = {"A1", "A2", "A3"}; String[] str2 = {"B1", "B2", "B3"}; String[] str3 = {"C1", "C2", "C3"}; Demo01 demo01 = new Demo01(); List<String> list1 = demo01.addList(str1); List<String> list2 = demo01.addList(str2); List<String> list3 = demo01.addList(str3); map.put("一班", list1); map.put("二班", list2); map.put("三班", list3); System.out.println("请输入班级名:"); String name = input.next(); List<String> l = map.get(name); Iterator<String> iterator = l.iterator(); for (int i= 0;i<l.size();i++){ if(iterator.hasNext()){ System.out.println(iterator.next()+"\n"); } } } }
练习3
package JUL.JUL.Jul17; public class Student { private String name; private int age; private String sex; public Student() { } public Student(String name, int age, String sex) { this.age = age; this.name = name; this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
package JUL.JUL.Jul17; import java.util.*; public class TestStudent { static Scanner input = new Scanner(System.in); public static void main(String[] args) { List<Student> list1 = new ArrayList<Student>(); List<Student> list2 = new ArrayList<Student>(); List<Student> list3 = new ArrayList<Student>(); for (int i = 1; i <= 5; i++) { Student stu1 = new Student("张" + i, 18, "男"); Student stu2 = new Student("李" + i, 18, "男"); Student stu3 = new Student("王" + i, 28, "女"); list1.add(stu1); list2.add(stu2); list3.add(stu3); } HashMap<String,List<Student>> has = new HashMap<String,List<Student>>(); has.put("一班",list1); has.put("二班",list2); has.put("三班",list3); System.out.println("请输入班级名:"); String name = input.next(); List<Student> list = has.get(name); System.out.println(name+"学生列表:"); for(Student st:list){ System.out.println(st.getName()+"\t"+st.getAge()+"\t"+st.getSex()); } } }