当前位置: 代码迷 >> Java相关 >> [求助]能不能讲讲关于HashMap的用法
  详细解决方案

[求助]能不能讲讲关于HashMap的用法

热度:121   发布时间:2006-08-22 17:47:16.0
[求助]能不能讲讲关于HashMap的用法

今天做了一个关于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;
}
}

搜索更多相关的解决方案: HashMap  用法  

----------------解决方案--------------------------------------------------------
把student类变成一个hashmap就行了。
另:输入姓名跟学号放在一个JOptionPane里就可以了。

----------------解决方案--------------------------------------------------------
  相关解决方案