袁弘 王凯 胡歌: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);
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;
}
}
不需要这么复杂的,只要这个程序能运行就可以拉,
而且没有回答我的问题
呵呵
代码如下;
package list;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MapTest {
public static void main(String[] args) {
Map<String, Employee> staff = new HashMap<String, Employee>();
staff.put("144-25-5464", new Employee("Amy"));
staff.put("567-24-2546", new Employee("Harry"));
staff.put("157-62-7935", new Employee("Gray"));
staff.put("456-62-5527", new Employee("France"));
System.out.println(staff);
staff.remove("567-24-2546");//删除
System.out.println(staff);
staff.put("456-62-5527", new Employee("Bob"));//替换
System.out.println(staff);
System.out.println(staff.get("157-62-7935"));//查询
//取得map中所有的key和value
for(Map.Entry<String, Employee> entry : staff.entrySet()) {
String key = entry.getKey();
Employee value = entry.getValue();
System.out.println("key=" + key + ", value=" + value + "");
}
//取得map中所有的key
Set<String> keys = staff.keySet();
for(String key : keys) {
System.out.println(key);
}
//取得map中所有的value
Collection<Employee> values = staff.values();
for(Employee value : values) {
System.out.println(value);
}
}
}
class Employee {
public Employee(String n) {
name = n;
salary = 0;
}
public String toString() {
return "[name=" + name + ", salary=" + salary + "]";
}
private String name;
private double salary;
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import com.bruceeckel.swing.*;
public class TrackEvent extends JApplet {
private HashMap h = new HashMap();
private String[] event = {
"focusGained", "focusLost", "keyPressed",
"keyReleased", "keyTyped", "mouseClicked",
"mouseEntered", "mouseExited", "mousePressed",
"mouseReleased", "mouseDragged", "mouseMoved"
};
private MyButton
b1 = new MyButton(Color.BLUE, "test1"),
b2 = new MyButton(Color.RED, "test2");
class MyButton extends JButton {
void report(String field, String msg) {
((JTextField)h.get(field)).setText(msg);
}
FocusListener fl = new FocusListener() {
public void focusGained(FocusEvent e) {
report("focusGained", e.paramString());
}
public void focusLost(FocusEvent e) {
report("focusLost", e.paramString());
}
};
KeyListener kl = new KeyListener() {
public void keyPressed(KeyEvent e) {
report("keyPressed", e.paramString());
}
public void keyReleased(KeyEvent e) {
report("keyReleased", e.paramString());
}
public void keyTyped(KeyEvent e) {
report("keyTyped", e.paramString());
}
};
MouseListener ml = new MouseListener() {
public void mouseClicked(MouseEvent e) {
report("mouseClicked", e.paramString());
}
public void mouseEntered(MouseEvent e) {
report("mouseEntered", e.paramString());
}
public void mouseExited(MouseEvent e) {
report("mouseExited", e.paramString());
}
public void mousePressed(MouseEvent e) {
report("mousePressed", e.paramString());
}
public void mouseReleased(MouseEvent e) {
report("mouseReleased", e.paramString());
}
};
MouseMotionListener mml = new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {
report("mouseDragged", e.paramString());
}
public void mouseMoved(MouseEvent e) {
report("mouseMoved", e.paramString());
}
};
public MyButton(Color color, String label) {
super(label);
setBackground(color);
addFocusListener(fl);
addKeyListener(kl);
addMouseListener(ml);
addMouseMotionListener(mml);
}
}
public void init() {
Container c = getContentPane();
c.setLayout(new GridLayout(event.length + 1, 2));
for(int i = 0; i < event.length; i++) {
JTextField t = new JTextField();
t.setEditable(false);
c.add(new JLabel(event[i], JLabel.RIGHT));
c.add(t);
h.put(event[i], t);
}
c.add(b1);
c.add(b2);
}
public static void main(String[] args) {
Console.run(new TrackEvent(), 700, 500);
}
}