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;
}
}