168 - 学生Map

Submit: 162  Solved: 119

Description

修改《学生列表》题目,使用学生Map来存放学生的集合,其中key为学号,value为学生对象
输出时按照学生的学号顺序输出

Input

学生个数
学生对象数据
操作数
操作内容

Output

按照学号顺序输出集合中的学生

Sample Input

4
1 wong 90
2 liu 80
3 chen 70
4 fang 60
3
add 5 duan 80
delete 3
set 4 70

Sample Output

no:1 name:wong score:90
no:2 name:liu score:80
no:4 name:fang score:70
no:5 name:duan score:80
import java.util.Scanner;

import java.util.List;

import java.util.LinkedList;

import java.util.Map;

import java.util.HashMap;

class Main {

    public static void main(String[] args) {

        Scanner scan=new Scanner(System.in);

        Map<Integer,Student> map = new HashMap<Integer,Student>();

        int Num = scan.nextInt();

        for(int i=0;i<Num;i++)

        {

            int no = scan.nextInt();

            String name = scan.next();

            int score = scan.nextInt();

            map.put(new Integer(no),new Student(no,name,score));

        }

        int op_num = scan.nextInt();

        int no;

        String name;

        int score;

        for(int i=0;i<op_num;i++)

        {

            String op = scan.next();

            switch(op)

            {

            case "add":

                no = scan.nextInt();

                name = scan.next();

                score = scan.nextInt();

                map.put(new Integer(no),new Student(no,name,score));

                break;

            case "delete":

                Integer num = new Integer(scan.nextInt());

                if(map.containsKey(num))

                    map.remove(num);

                break;

            case "set":

                Integer num2 = new Integer(scan.nextInt());

                score = scan.nextInt();

                if(map.containsKey(num2))

                    map.get(num2).score = score;

                break;

            }

//            if(op == "add")

//            {

//                no = scan.nextInt();

//                name = scan.next();

//                score = scan.nextInt();

//                list.add(new Student(no,name,score));

//            }

//            if(op == "delete")

//            {

//                no = scan.nextInt();

//                for(int j=0;j<list.size();j++)

//                {

//                    if(list.get(j).no == no)

//                        list.remove(j);

//                }

//            }

//            if(op == "set")

//            {

//                no = scan.nextInt();

//                score = scan.nextInt();

//                for(int j=0;j<list.size();j++)

//                {

//                    if(list.get(j).no == no)

//                        list.get(j).score = score;

//                }

//            }

        }

        for(Integer i:map.keySet())

        {

            Student s = (Student)map.get(i);

            System.out.println(s.toString());

        }

        scan.close();

    }

}

class Student

{

    public int no;

    public String name;

    public int score;

    public Student(int _no,String _name,int _score)

    {

        this.no = _no;

        this.name = _name;

        this.score = _score; 

    }

    public int getNo()

    {

        return this.no;

    }

    @Override

    public boolean equals(Object O)

    {

        Student T = (Student) O;

        return (this.getNo() == T.getNo() )?true:false;

    }

    @Override

    public String toString()

    {

        return "no:"+no+" name:"+name+" score:"+score;

    }

    @Override

    public int hashCode()

    {

        int result = 17;

        result = 31*result+no;

        return result;

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值