HashMap基本用法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HashMap基本用法相关的知识,希望对你有一定的参考价值。

参考技术A 分类: 电脑/网络 >> 程序设计 >> 其他编程语言
问题描述:

要用hashMap方法,我一用PUT就出错,帮看看哪里有错啊;最好讲讲HASHMAP的用法import javax.swing.*;import java.util.*;public class StudetHashMappublic 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;doint 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 Studentprivate 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;

解析:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import .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);



以上是关于HashMap基本用法的主要内容,如果未能解决你的问题,请参考以下文章

Java HashMap用法

HashMap概述与用法总结

java Integer用法

int 与Integer的用法与区别

Java List 用法

Java中DataInputStream的用法