尚学堂Java第9章作业编码题参考

    xiaoxiao2023-11-02  140

    本人初学JAVA,自己写的答案,仅供参考。如有错误或可以优化的地方请各位留言指正。谢谢!*

    1.使用List和Map存放多个图书信息,遍历并输出。其中商品属性:编号,名称,单价,出版社;使用商品编号作为Map中的key。

    `` public class Test1 { public static void main(String[] args) { //创建图书对象 Books book1 = new Books(1001,“时间简史”,77,“新华出版社”); Books book2 = new Books(1002,“三体”,66,“邮电出版社”); Books book3 = new Books(1003,“流浪地球”,55,“中国人民出版社”); //list集合 List list = new ArrayList(); list.add(book1); list.add(book2); list.add(book3); //遍历list集合 for (Books book : list) { System.out.println(book); } //map集合 HashMap<Integer,Books> map = new HashMap<Integer,Books>(); map.put(book1.getNumber(), book1); map.put(book2.getNumber(), book2); map.put(book3.getNumber(), book3); //遍历map集合 Set<Entry<Integer, Books>> set = map.entrySet(); for (Entry<Integer, Books> entry : set) { System.out.println(“编号” + entry.getKey() + “:” + entry.getValue()); } } }

    ```图书类 public class Books { private int number; private String name; private double price; private String press; public Books(int nums, String name, double price, String press) { super(); this.number = nums; this.name = name; this.price = price; this.press = press; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } @Override public String toString() { return "Books [number=" + number + ", name=" + name + ", price=" + price + ", press=" + press + "]"; } }

    2.使用HashSet和TreeSet存储多个商品信息,遍历并输出;其中商品属性:编号,名称,单价,出版社;要求向其中添加多个相同的商品,验证集合中元素的唯一性。

    public class Test1 { public static void main(String[] args) { //创建图书对象 Books book1 = new Books(1001,"时间简史",77,"新华出版社"); Books book2 = new Books(1002,"三体",66,"邮电出版社"); Books book3 = new Books(1003,"流浪地球",55,"中国人民出版社"); Books book4 = new Books(1003,"流浪地球",55,"中国人民出版社"); //HashSet集合 HashSet<Books> hs = new HashSet<Books>(); hs.add(book1); hs.add(book2); hs.add(book3); hs.add(book4); //遍历HashSet for (Books books : hs) { System.out.println(books); } System.out.println("=========================="); //TreeSet TreeSet<Books> ts = new TreeSet<Books>(); ts.add(book1); ts.add(book2); ts.add(book3); ts.add(book4); //遍历TreeSet for (Books books : ts) { System.out.println(books); } } }

    !输出结果

    3. 实现List和Map数据的转换。具体要求如下: 功能1:定义方法public void listToMap( ){ }将List中Student元素封装到Map中 1) 使用构造方法Student(int id,String name,int age,String sex )创建多个学生信息并加入List; 2) 遍历List,输出每个Student信息; 3) 将List中数据放入Map,使用Student的id属性作为key,使用Student对象信息作为value; 4) 遍历Map,输出每个Entry的key和value。 功能2:定义方法public void mapToList( ){ }将Map中Student映射信息封装到List 1) 创建实体类StudentEntry,可以存储Map中每个Entry的信息; 2) 使用构造方法Student(int id,String name,int age,String sex )创建多个学生信息,并使用Student的id属性作为key,存入Map; 3) 创建List对象,每个元素类型是StudentEntry; 4) 将Map中每个Entry信息放入List对象。

    学生类:

    public class Student { private int id; private String name; private int age; private String sex; public Student(int id, String name, int age, String sex) { super(); this.id = id; this.name = name; this.age = age; this.sex = sex; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]"; } } 功能1: public class Test2 { public static void main(String[] args) { //使用构造方法Student(int id,String name,int age,String sex )创建多个学生信息并加入List; Student s1 = new Student(1,"张三",20,"男"); Student s2 = new Student(2,"李四",22,"男"); Student s3 = new Student(3,"王五",18,"女"); List<Student> list = new ArrayList<Student>(); list.add(s1); list.add(s2); list.add(s3); //遍历List,输出每个Student信息; for (Student s : list) { System.out.println(s); } //遍历Map,输出每个Entry的key和value。 Map<Integer,Student> map = new HashMap<Integer,Student>(); map = listToMap(list, map); Set<Entry<Integer, Student>> set = map.entrySet(); for (Entry<Integer, Student> entry : set) { System.out.println("ID:" + entry.getKey() + " 学生信息:" + entry.getValue()); } } //将List中数据放入Map,使用Student的id属性作为key,使用Student对象信息作为value; public static Map<Integer,Student> listToMap(List<Student> list,Map<Integer,Student> map) { for(int i = 0;i < list.size();i++) { map.put(list.get(i).getId(), list.get(i)); } return map; } }

    功能2:

    public class Test2 { public static void main(String[] args) { //使用构造方法Student(int id,String name,int age,String sex )创建多个学生信息并加入List; Student s1 = new Student(1,"张三",20,"男"); Student s2 = new Student(2,"李四",22,"男"); Student s3 = new Student(3,"王五",18,"女"); //使用Student的id属性作为key,存入Map; Map<Integer,Student> map = new HashMap<Integer,Student>(); map.put(s1.getId(), s1); map.put(s2.getId(), s2); map.put(s3.getId(), s3); //创建List对象,每个元素类型是StudentEntry; List<StudentEntry> list = new ArrayList<StudentEntry>(); list = mapToList(list, map); for (StudentEntry s : list) { System.out.println(s.toString()); } } //将Map中每个Entry信息放入List对象。 public static List<StudentEntry> mapToList(List<StudentEntry> list,Map<Integer,Student> map){ Set<Entry<Integer, Student>> set = map.entrySet(); list.add(new StudentEntry(map,set)); return list; } } public class StudentEntry { public Map<Integer,Student> map = new HashMap<Integer,Student>(); public Set<Entry<Integer, Student>> set = map.entrySet(); public StudentEntry(Map<Integer, Student> map, Set<Entry<Integer, Student>> set) { super(); this.map = map; this.set = set; } @Override public String toString() { return "StudentEntry [set=" + set + "]"; } }
    最新回复(0)