JAVA初学——week12集合课后练习题

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

package test1;
//1、使用链表和映射存放多个图书信息,遍历并输出。其中商品属性:编号,名称,单价,出版社;使用商品编号作为映射中的key。
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
public class map114514 {
    static class Book{
        private String index;//编号
        private String name;//名称
        private int price;//单价
        private String press;//出版社
        public Book(String index,String name,int price,String press){
            this.index = index;//编号
            this.name = name;//名称
            this.price = price;//单价
            this.press = press;//出版社
        }
        public String getIndex(){
            return this.index;
        }
        public String getName(){
            return this.name;
        }
        public int getPrice(){
            return this.price;
        }
        public String getPress(){
            return this.press;
        }
        public void setIndex(String index){
            this.index = index;
        }
        public void setName(String name){
            this.name = name;
        }
        public void setPrice(int price){
            this.price = price;
        }
        public void setPress(String press){
            this.press = press;
        }
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        List<Book> books = new ArrayList<Book>();
        Map<String,Book> books1 = new HashMap<String,Book>();
        System.out.print("请输入书本数量:");
        int n = input.nextInt();
        for(int i = 0;i < n;++ i){
            String name,index,press,str;
            int price;
            index = input.next();//书本编号
            name = input.next();//名称
            price = input.nextInt();//单价
            press = input.next();//出版社
            Book x = new Book(index,name,price,press);
            books.add(x);
            books1.put(index,x);
        }
        //遍历所有书本
        Iterator it = books.iterator();
        while(it.hasNext()){
            Book book = (Book) it.next();
            System.out.println(book.getName());
        }
        //遍历所有编号映射的书
        Iterator it1 = books1.entrySet().iterator();
        while(it1.hasNext()){
            Map.Entry entry = (Map.Entry)it1.next();
            System.out.println("next :" + entry.getKey() + " " + ((Book)entry.getValue()).getName());
        }
    }
}

2、由控制台按照固定格式输入学生信息,包括学号,姓名,年龄信息,当输入的内容为exit退出;将输入的学生信息分别封装到一个Student对象中,再将每个Student对象加入到一个集合中,要求集合中的元素按照年龄大小正序排序;最后遍历集合,将集合中学生信息写入到记事本,每个学生数据占单独一行

package test2;

import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;
import java.util.Set;
//2、由控制台按照固定格式输入学生信息,包括学号,姓名,年龄信息,当输入的内容为exit退出;
// 将输入的学生信息分别封装到一个Student对象中,
// 再将每个Student对象加入到一个集合中,
// 要求集合中的元素按照年龄大小正序排序;
// 最后遍历集合,将集合中学生信息写入到记事本,每个学生数据占单独一行
public class StudentInformation {
    static class Student implements Comparable{
        String index;//学号
        String name;//姓名
        int age;//年龄信息
        public Student(String index,String name,int age){
            this.index = index;//编号
            this.name = name;//名称
            this.age = age;//年龄
        }
        public String getIndex(){
            return this.index;
        }
        public void setIndex(String index){
            this.index = index;
        }
        public String getName(){
            return this.name;
        }
        public void setName(String name){
            this.name = name;
        }
        public int getAge(){
            return this.age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public int compareTo(Object o) {
            if(!(o instanceof Student)){
                throw new RuntimeException("不是Student对象");
            }
            Student p = (Student) o;
            if(this.age > p.age){
                return 1;
            }else if(this.age == p.age){
                return this.name.compareTo(p.name);
            }else{
                return -1;
            }
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Set<Student> students = new TreeSet<Student>();
        while(true){
            String index = input.next();
            if("exit".equals(index)){
                break;
            }
            String name = input.next();
            int age = input.nextInt();
            Student x = new Student(index,name,age);
            students.add(x);
        }
        Iterator<Student> it = students.iterator();
        while(it.hasNext()){
            Student student = (Student)it.next();
            System.out.println("name:" + student.getName());
        }
    }
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值