今天做了一个关于HashMap的题目
已经把ArrayList的做出来了要改成HashMap的
import javax.swing.*;
import java.util.*;
public class StudetHashMap{
public static void main(String args[])
{
HashMap stu=new HashMap();
System.out.println ("欢迎进入学生系统");
System.out.println ("1.添加学生");
System.out.println ("2.查询学生");
System.out.println ("3.删除学生");
int c=0;
do
{
int choice=Integer.parseInt(JOptionPane.showInputDialog(null,"请输入你的选择"));
switch(choice)
{
case 1:
Student newstu=null;
String code=JOptionPane.showInputDialog(null,"请你输入学号");
String name=JOptionPane.showInputDialog(null,"请你输入姓名");
newstu=new Student(code,name);
arr.add(newstu);
break;
case 2:
String codes=JOptionPane.showInputDialog(null,"请你输入学号");
Student temp=null;
for (int i = 0; i<stu.size(); i++)
{
temp=(Student)stu.get(i);
if(temp.getCode().equals(codes))
{
break;
}
}
System.out.println (temp.getName());
break;
case 3:
String codee=JOptionPane.showInputDialog(null,"请输入学号");
Student temps=null;
int i=0;
for ( i = 0; i<stu.size(); i++)
{
temps=(Student)stu.get(i);
if(temps.getCode().equals(codee))
{
break;
}
}
stu.remove(i);
}
c=Integer.parseInt(JOptionPane.showInputDialog(null,"继续吗?1.继续\n2.不继续"));
}while(c==1);
}
}
class Student
{
private String name;
private String code;
public Student(String code,String name)
{
this.name=name;
this.code=code;
}
public void setCode(String code)
{
this.code=code;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public String getCode()
{
return code;
}
}